Hello I am trying to sort a set of numeric command line arguments and then echo them back out in reverse numeric order on the same line with a space between each. I have this loop:
for var in "$@"
do
echo -n "$var "
done | sort -rn
However when I added the -n
to the echo the sort
command stops working. I am trying to do this without using printf
. Using the echo -n
they do not sort and simply print in the order they were entered.
You can do it like this:
a=( $@ )
b=( $(printf "%s\n" ${a[@]} | sort -rn) )
printf "%s\n" ${b[@]}
# b is reverse sorted nuemrically now
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With