Using command line argument range in bash for loop prints brackets containing the arguments

ru4mqart668op514 picture ru4mqart668op514 · Apr 17, 2011 · Viewed 52.4k times · Source

It's probably a lame question. But I am getting 3 arguments from command line [ bash script ]. Then I am trying to use these in a for loop.

for i in {$1..$2}
    do action1
done

This doesn't seem to work though and if $1 is "0" and $2 is 2 it prints {0..2}' and callsaction1` only once. I referred to various examples and this appears to be the correct usage. Can someone please tell me what needs to be fixed here?

Thanks in advance.

Answer

Vijayender picture Vijayender · Oct 14, 2011

You can slice the input using ${@:3} or ${@:3:8} and then loop over it

For eg., to print arguments starting from 3

for i in ${@:3} ; do echo $i; done

or to print 8 arguments starting from 3 (so, arguments 3 through 10)

for i in ${@:3:8} ; do echo $i; done