# Programming Tutorials

## Sort numbers using arrays in Shell Script

By: Vivek G Printer Friendly Format

This shell script demonstrates the use of arrays with a script that sorts 5 numbers and prints the sorted numbers. ``` # # Declare the array of 5 subscripts to hold 5 numbers # declare nos[5]=(4 -1 2 66 10) # # Prints the number befor sorting # echo "Original Numbers in array:" for (( i = 0; i <= 4; i++ )) do echo \${nos[\$i]} done # # Now do the Sorting of numbers # for (( i = 0; i <= 4 ; i++ )) do for (( j = \$i; j <= 4; j++ )) do if [ \${nos[\$i]} -gt \${nos[\$j]} ]; then t=\${nos[\$i]} nos[\$i]=\${nos[\$j]} nos[\$j]=\$t fi done done # # Print the sorted number # echo -e "\nSorted Numbers in Ascending Order:" for (( i=0; i <= 4; i++ )) do echo \${nos[\$i]} done ```

 Most Viewed Articles (in Linux ) Latest Articles (in Linux)

Comment on this tutorial

#### Subscribe to Tutorials

1. thanks ...
View Tutorial          By: Dipesh at 2011-09-07 17:05:21

2. how to declare Array ?? in LINUX
i m try th

View Tutorial          By: Chand at 2012-03-18 06:47:09

3. its very nice
View Tutorial          By: keerthi at 2012-06-28 17:04:36

4. hii
i have

two varirables
View Tutorial          By: niranjan at 2012-07-31 06:45:46

5. hii
i have

two varirables
View Tutorial          By: niranjan at 2012-07-31 06:47:36

6. does not work.

output:
./num

View Tutorial          By: praveen at 2012-08-30 19:04:20

7. This will give the output check it out

View Tutorial          By: shruthi at 2013-01-18 06:00:08

8. hahaha
View Tutorial          By: seshu babu at 2015-04-20 06:29:14

9. blank
View Tutorial          By: veera venkata satya naga maha veera ambika trimurthulu at 2015-04-20 06:32:54