Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting bash arguments alphabetically

Tags:

bash

How can I take sort bash arguments alphabetically?

$ ./script.sh bbb aaa ddd ccc

and put it into an array such that I now have an array {aaa, bbb, ccc, ddd}

like image 209
ehaydenr Avatar asked Jan 10 '23 15:01

ehaydenr


1 Answers

You can do:

A=( $(sort <(printf "%s\n" "$@")) )

printf "%s\n" "${A[@]}"
aaa
bbb
ccc
ddd

It is using steps:

  1. sort the arguments list i.e."$@"`
  2. store output of sort in an array
  3. Print the sorted array
like image 52
anubhava Avatar answered Jan 18 '23 08:01

anubhava