0

How can I traverse through 2nd argument to last argument like:

for arg in $2-$@
do
  echo $i
done

Please help

1

3 Answers 3

2

Actually, don't trust the arguments... they may contain spaces, or other special meta characters to the shell. Double quotes are your friends in shell scripts.

shift;           #eat $1
for arg in "$@"
do
  echo "$arg"
done

When put in double quotes the "$@" takes on special magic to assure words are retained. Much better then $*. "$*" would process all arguments at once.

Double quotes are not a perfect solution, just the best easy one.

2

Use shift in bash.

shift
for arg in $@
do
  echo $arg
done
0
args=("$@")

for arg in $(seq 2 `expr $# - 1`)
do
  echo ${args[$arg]}
done

Not the answer you're looking for? Browse other questions tagged or ask your own question.