Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8328421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:26:21+00:00 2026-06-09T01:26:21+00:00

I have an array in Bash, for example: array=(a c b f 3 5)

  • 0

I have an array in Bash, for example:

array=(a c b f 3 5)

I need to sort the array. Not just displaying the content in a sorted way, but to get a new array with the sorted elements. The new sorted array can be a completely new one or the old one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:26:23+00:00Added an answer on June 9, 2026 at 1:26 am

    You don’t really need all that much code:

    IFS=$'\n' sorted=($(sort <<<"${array[*]}"))
    unset IFS
    

    Supports whitespace in elements (as long as it’s not a newline), and works in Bash 3.x.

    e.g.:

    $ array=("a c" b f "3 5")
    $ IFS=$'\n' sorted=($(sort <<<"${array[*]}")); unset IFS
    $ printf "[%s]\n" "${sorted[@]}"
    [3 5]
    [a c]
    [b]
    [f]
    

    Note: @sorontar has pointed out that care is required if elements contain wildcards such as * or ?:

    The sorted=($(…)) part is using the “split and glob” operator. You should turn glob off: set -f or set -o noglob or shopt -op noglob or an element of the array like * will be expanded to a list of files.

    What’s happening:

    The result is a culmination six things that happen in this order:

    1. IFS=$'\n'
    2. "${array[*]}"
    3. <<<
    4. sort
    5. sorted=($(...))
    6. unset IFS

    First, the IFS=$'\n'

    This is an important part of our operation that affects the outcome of 2 and 5 in the following way:

    Given:

    • "${array[*]}" expands to every element delimited by the first character of IFS
    • sorted=() creates elements by splitting on every character of IFS

    IFS=$'\n' sets things up so that elements are expanded using a new line as the delimiter, and then later created in a way that each line becomes an element. (i.e. Splitting on a new line.)

    Delimiting by a new line is important because that’s how sort operates (sorting per line). Splitting by only a new line is not-as-important, but is needed preserve elements that contain spaces or tabs.

    The default value of IFS is a space, a tab, followed by a new line, and would be unfit for our operation.

    Next, the sort <<<"${array[*]}" part

    <<<, called here strings, takes the expansion of "${array[*]}", as explained above, and feeds it into the standard input of sort.

    With our example, sort is fed this following string:

    a c
    b
    f
    3 5
    

    Since sort sorts, it produces:

    3 5
    a c
    b
    f
    

    Next, the sorted=($(...)) part

    The $(...) part, called command substitution, causes its content (sort <<<"${array[*]}) to run as a normal command, while taking the resulting standard output as the literal that goes where ever $(...) was.

    In our example, this produces something similar to simply writing:

    sorted=(3 5
    a c
    b
    f
    )
    

    sorted then becomes an array that’s created by splitting this literal on every new line.

    Finally, the unset IFS

    This resets the value of IFS to the default value, and is just good practice.

    It’s to ensure we don’t cause trouble with anything that relies on IFS later in our script. (Otherwise we’d need to remember that we’ve switched things around–something that might be impractical for complex scripts.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say we have array of arrays: tree_limbs = Array.new tree_limbs << %w(1 2 3
I am trying to declare an array in bash, but when the code is
I have a strange issue with array manipulation within a bash script on Solaris.
So if I have a bash array: ar=( one two three four) What is
Bash 4 on Linux ~ I have an array of possible values. I must
I have an indexed bash array and I'd like to use an expression like
i have short bash code cat example.txt | grep mail | awk -F, '{print
I have an array in Bash, say it contains the numbers {1, 2, 3,
In my bash script, I have an array of filenames like files=( site_hello.xml site_test.xml
I have the following example array: $comparisons = array( 0 => array( 'method' =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.