Iterate over the argument list in linux shell

I want to iterate over a list of arguments in a shell, I know how to do this with

for var in $@ 

But I want to do it with

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

I need this because the first two arguments will not go into the loop. Does anyone know how to do this? We are waiting for you to help.

cheng

+6
source share
3 answers

This can help:

 for var in "${@:3}" 

for more information you can see:

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

+9
source

reader_1000 provides a nice bash spell , but if you use an older (or simpler) Bourne shell, you can use creaky ancients (and therefore very portable)

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

Although this is an old question, there is another way to do this. And perhaps 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 
+1
source

Source: https://habr.com/ru/post/898442/


All Articles