Why doesn’t this print all the passed arguments, in bash?
function abc() {
echo "$1" #prints the correct argument
for x in `seq 1 $#`; do
echo "$x" #doesn't print the 1st, 2nd, etc arguments, but instead 1, 2, ..
done
}
It is printing
1
2
3
4
...
instead.
I’ll just add a couple more options to what everyone else has given. The closest to the way you’re trying to write this is to use bash indirect expansion:
…another option if you want to operate on only some of the arguments, is to use array slicing with $@:
similarly,
"${@:2}"will give you all arguments starting at number 2,"${@:$start:$((end-start+1))}"will give you arguments $start through $end (the$((expression calculates how many arguments there are between $start and $end), etc…