user572138
user572138

Reputation: 463

iterate over argument list in linux shell

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

Upvotes: 7

Views: 11142

Answers (3)

shady
shady

Reputation: 455

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

Upvotes: 1

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 "$@"
...

Upvotes: 4

reader_1000
reader_1000

Reputation: 2501

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

Upvotes: 11

Related Questions