top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How would you write a shell script that prints all the additional arguments passed to it in reverse order?

+1 vote
193 views
How would you write a shell script that prints all the additional arguments passed to it in reverse order?
posted Feb 18, 2016 by Mohammed Hussain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
for (( i = ${#}; i > 0; i-- )); do
        echo ${!i}
done

The arguments are available as $, where n is the position of the argument. For example, $0 would give the name of the script, $1 would give the first additional argument, $2 the second, and so on. The total number of additional arguments is found in $#.

A loop that starts at $# and ends at 1 can be used to print each additional argument in reverse order.

answer Feb 25, 2016 by Manikandan J
...