You should use the for arg
form that others have shown. However, to address some things in your question and comments, see the following:
In Bash, it's not necessary to use seq
. You can use C-style for
loops:
for ((i = 2; i <= $#; i++))
do
echo "${@:i:1}"
done
Which demonstrates array slicing which is another technique you can use in addition to direct iteration (for arg
) or using shift
.
An advantage of using either version of for
is that the argument array is left intact, while shift modifies it. Also, with the C-style form with array slicing, you could skip any arguments you like. This is usually not done to the extent shown below, because it would rely on the arguments following a strict pattern.
for ((i = 2; i < $# - 2; i+=2))
That bit of craziness would start at the second argument, process every other one and stop before the last two or three (depending on whether $#
is odd or even).