Bash Script to Sort Array in Ascending Order
Use
sorted=($(printf "%s\n" "${array[@]}" | sort -n)) to sort a Bash array in ascending order and store the result in a new array.Examples
Inputarray=(3 1 2)
Outputsorted=(1 2 3)
Inputarray=(10 5 8 1)
Outputsorted=(1 5 8 10)
Inputarray=()
Outputsorted=()
How to Think About It
To sort an array in Bash, print each element on a new line, use the
sort -n command to order them numerically, then read the sorted lines back into an array. This uses Bash's ability to handle command substitution and arrays.Algorithm
1
Take the original array.2
Print each element on a separate line.3
Use the <code>sort -n</code> command to sort these lines in ascending numerical order.4
Capture the sorted output back into a new array.5
Print or use the sorted array as needed.Code
bash
array=(3 1 2 10 5) sorted=($(printf "%s\n" "${array[@]}" | sort -n)) echo "Sorted array: ${sorted[@]}"
Output
Sorted array: 1 2 3 5 10
Dry Run
Let's trace sorting the array (3 1 2 10 5) through the code
1
Original array
array=(3 1 2 10 5)
2
Print elements line by line
3 1 2 10 5
3
Sort output
1 2 3 5 10
4
Read sorted into array
sorted=(1 2 3 5 10)
| Step | Array Content |
|---|---|
| 1 | 3 1 2 10 5 |
| 2 | 3 1 2 10 5 |
| 3 | 1 2 3 5 10 |
| 4 | 1 2 3 5 10 |
Why This Works
Step 1: Print each element on a new line
Using printf "%s\n" "${array[@]}" sends each array element to a separate line, preparing it for sorting.
Step 2: Sort the lines
The sort -n command sorts the lines numerically in ascending order.
Step 3: Capture sorted output into array
Using sorted=($(...)) reads the sorted lines back into a Bash array for easy use.
Alternative Approaches
Using a for loop with bubble sort
bash
array=(3 1 2 10 5) for ((i=0; i<${#array[@]}; i++)); do for ((j=0; j<${#array[@]}-i-1; j++)); do if (( array[j] > array[j+1] )); then temp=${array[j]} array[j]=${array[j+1]} array[j+1]=$temp fi done done echo "Sorted array: ${array[@]}"
This manual sorting is slower and more complex but does not rely on external commands.
Using mapfile and sort
bash
array=(3 1 2 10 5) mapfile -t sorted < <(printf "%s\n" "${array[@]}" | sort -n) echo "Sorted array: ${sorted[@]}"
Uses <code>mapfile</code> to read sorted lines into an array, which is cleaner but requires Bash 4+.
Complexity: O(n log n) time, O(n) space
Time Complexity
The sorting uses the external sort command which typically runs in O(n log n) time, where n is the number of elements.
Space Complexity
Extra space is used to hold the printed lines and the sorted array, so space complexity is O(n).
Which Approach is Fastest?
Using the sort command is faster and simpler than manual sorting loops in Bash, especially for larger arrays.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Using sort command | O(n log n) | O(n) | Most cases, simple and fast |
| Manual bubble sort loop | O(n²) | O(1) | Small arrays or no external commands |
| Using mapfile with sort | O(n log n) | O(n) | Cleaner syntax, Bash 4+ required |
Use
printf "%s\n" "${array[@]}" | sort -n to easily sort arrays numerically in Bash.Trying to sort arrays directly without printing elements line by line causes errors because Bash arrays can't be sorted in place.