tags:

views:

37

answers:

5

Hi i have the following:

bash_script parm1 a b c d ..n

I want to iterate and print all the values in the command line starting from a, not from parm1

A: 

This should do it:

#ignore first parm1
shift

# iterate
while test $# -gt 0
do
  echo $1
  shift
done

(ignore the coloration error on $#)

Scharron
ecelletn thanx :)what does -gt do ?Sorry I know to lazy to google
piet
greater than. just a comparison operator for test :-)
Scharron
Wow, that's pretty lazy. Try `help test` which is only 9 characters and a <return>. Good luck.
msw
man test is 8 .
Scharron
A: 

Another flavor, a bit shorter that keeps the arguments list

shift
for i in $*
do
  echo $i
done
ring0
A: 

You can "slice" arrays in bash; instead of using shift, you might use

for i in ${@:2}
do
    echo $i
done

$@ is an array of all the command line arguments, ${@:2} is the same array less the first element.

isbadawi
A: 

This method will keep the first param, in case you want to use it later

#!/bin/bash

for ((i=2;i<=$#;i++))
do
  echo ${!i}
done

or

for i in ${*:2} #or use $@
do
  echo $i
done
ghostdog74
A: 

You can use an implicit iteration for the positional parameters:

shift
for arg
do
    something_with $arg
done

As you can see, you don't have to include $* in the for statement.

Dennis Williamson