7

I want to iterate over the argument list in shell, I know how to do this with

for var in $@

But I want to do this with

for ((i=3; i<=$#; i++))

I need this because the first two arguments won't enter into the loop. Anyone knows how to do this? Looking forward to you help.

cheng

3 Answers 3

11

This might help:

for var in "${@:3}"

for more information you can look at:

http://www.ibm.com/developerworks/library/l-bash-parameters/index.html

Sign up to request clarification or add additional context in comments.

Comments

5

reader_1000 provides a nice bash incantation, but if you are using an older (or simpler) Bourne shell you can use the creaking ancient (and therefore highly portable)

VAR1=$1
VAR2=$2
shift 2
for arg in "$@"
...

Comments

1

Though this is an old question, there is another way you can do this. And, maybe this is what you are asking for.

for(( i=3; i<=$#; i++ )); do  
    echo "parameter: ${!i}"  #Notice the exclamation here, not the $ dollar sign.
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.