Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterate over argument list in linux shell

Tags:

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

like image 807
user572138 Avatar asked Oct 01 '11 13:10

user572138


2 Answers

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

like image 183
reader_1000 Avatar answered Oct 19 '22 03:10

reader_1000


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 "$@"
...
like image 35
dmckee --- ex-moderator kitten Avatar answered Oct 19 '22 05:10

dmckee --- ex-moderator kitten