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?
Simply:
#!/bin/sh
o=
for i;do
o="$i $o"
done
echo "$o"
will work as
./rev.sh 1 2 3 4
4 3 2 1
Or
./rev.sh world! Hello
Hello world!
Just replace echo
by printf "%s\n"
:
#!/bin/sh
o=
for i;do
o="$i $o"
done
printf "%s\n" $o
If your argument could contain spaces, you could use bash arrays:
#!/bin/bash
declare -a o=()
for i;do
o=("$i" "${o[@]}")
done
printf "%s\n" "${o[@]}"
Sample:
./rev.sh "Hello world" print will this
this
will
print
Hello world