tags:

views:

56

answers:

3

If you have a list in python, and you want the elements from 2 to n can do something nice like

list[2:]

I'd like to something similar with argv in Bash. I want to pass all the elements from $2 to argc to a command. I currently have

command $2 $3 $4 $5 $6 $7 $8 $9

but this is less than elegant. Would would be the "proper" way

+3  A: 

Store $1 somewhere, then shift and use $@?

isbadawi
good suggestion, but i think an operation on a list would be more elegant
Mike
A: 

script1.sh:

#!/bin/bash
echo $@

script2.sh:

#!/bin/bash
shift
echo $@

$ sh script1.sh 1 2 3 4
1 2 3 4 
$ sh script2.sh 1 2 3 4 
2 3 4
Tom
+2  A: 

you can do "slicing" as well, $@ gets all the arguments in bash.

echo ${@:2}

gets 2nd argument onwards

eg

$ cat shell.sh
#!/bin/bash
echo ${@:2}

$ ./shell.sh 1 2 3 4
2 3 4
ghostdog74
You should also wrap it in double-quotes to avoid problems with spaces in arguments (e.g. `echo "${@:2}"` would work properly when called with `./shell.sh 1 2 "multi-word argument" 4 5`).
Gordon Davisson