bash

Print bash arguments in reverse order


I have to write a script, which will take all arguments and print them in reverse.

I've made a solution, but find it very bad. Do you have a smarter idea?

#!/bin/sh
> tekst.txt

for i in $* 
do
    echo $i | cat - tekst.txt > temp && mv temp tekst.txt
done

cat tekst.txt

Solution

  • Could do this

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

    This uses the below
    c style for loop
    Parameter indirect expansion (${!i}towards the bottom of the page)

    And $# which is the number of arguments to the script