Let's define your arguments:
$ set -- one two three
Now, let's print them out in reverse order:
$ for ((i=$#;i>=1;i--)); do echo "${!i}"; done
three
two
one
How it works
for ((i=$#;i>=1;i--))
starts a loop in which i
counts down from $#
to 1. For each value of i
, we print the corresponding positional parameter by ${!i}
. The construct ${!i}
uses indirection: instead of returning the value of i
, ${!i}
returns the value of the variable whose name is $i
.
As a script
In a multi-line script form, we can use:
$ cat reverse
#!/bin/bash
for ((i=$#;i>=1;i--))
do
echo "${!i}"
done
As an example:
$ bash reverse One Two Three
Three
Two
One
Alternative: using tac
Another way to print things in reverse order is to use the utility tac
. Consider this script:
$ cat reverse2
#!/bin/bash
printf "%s\n" "$@" | tac
Here is an example:
$ bash reverse2 Uno Dos Tres
Tres
Dos
Uno
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.