This question already has an answer here:
- Print bash arguments in reverse order 4 answers
I was trying to write a script that print the arguments in reverse order. So if I type bash reverse.sh one two three I expect my output to be three two one How can i do this? This is what I tried and it obviously didn't work...
#!/bin/bash
i=0
a="$"
for word in $*; do
echo $a$(($#-i))
i=$((i+1))
done
This is the output i get
$3
$2
$1
I thought this would print the parameters in order 3, 2, 1 but it didn't. How should I do it? Any help will be much appreciated. Thank you.
You need
eval
withecho
i.e. you need to evaluate the expansion, not output it:Note that, using
eval
in general is discouraged as this could result in security implications if the input string is not sanitized. Check John1024's answer to see how this can be done withouteval
.Let's define your arguments:
Now, let's print them out in reverse order:
How it works
for ((i=$#;i>=1;i--))
starts a loop in whichi
counts down from$#
to 1. For each value ofi
, we print the corresponding positional parameter by${!i}
. The construct${!i}
uses indirection: instead of returning the value ofi
,${!i}
returns the value of the variable whose name is$i
.As a script
In a multi-line script form, we can use:
As an example:
Alternative: using tac
Another way to print things in reverse order is to use the utility
tac
. Consider this script:Here is an example:
printf "%s\n" "$@"
prints out the positional parameters one per line.tac
prints those lines in reverse order.Limitation: The
tac
method only works correctly if the arguments do not themselves contain newlines.