I need to write a bash script that prints out its command line arguments in sorted order, one per line.
I wrote this script and it works fine, but is there any other way? Especially without outputting it to a file and sorting.
#!/bin/bash
for var in $*
do
echo $var >> file
done
sort file
rm file
Test run of the program:
$ script hello goodbye zzz aa
aa
goodbye
hello
zzz
You want to use
$@in quotes (instead of$*) to accommodate arguments with spaces, such asThe pipe gets rid of the need for a temporary file.