Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - Sort a list of strings

Tags:

bash

list

sorting

Would you please show me know how I can sort the following list (ascending oder A to Z) (or a list in general) with Bash?

I have been trying but still could not get the expected results:

my_list='a z t b e c'

And the result should be a list as well, as I will use it for Select Loop.

my_list='a b c e t z'  

Thanks for your help!

like image 499
Hung Tran Avatar asked Nov 30 '22 22:11

Hung Tran


1 Answers

You can use xargs twice along with a built in sort command to accomplish this.

$ my_list='a z t b e c'
$ my_list=$(echo $my_list | xargs -n1 | sort | xargs)
$ echo $my_list
a b c e t z
like image 139
Alex Stiff Avatar answered Dec 04 '22 06:12

Alex Stiff