How can I access the values within $@ starting from the third one? Right now I'm passing them from three to nine, but I think theres a better way:
while getopts ":n" opt "$3 $4 $5 $6 $7 $8 $9"; do
How can I access the values within $@ starting from the third one? Right now I'm passing them from three to nine, but I think theres a better way:
while getopts ":n" opt "$3 $4 $5 $6 $7 $8 $9"; do
Seems like a funky approach to arguments to me, but:
[kbrandt@kbrandt: ~/scrap] cat args
args=("$@")
echo ${args[0]}
echo ${args[@]:1:2}
echo ${args[@]:0:$#}
[kbrandt@kbrandt: ~/scrap] bash args foo bar baz biz
foo
bar baz
foo bar baz biz
I recommend you check out the FAQ Answer about command line arguments (Which basically says getopts or loop/case/shift).
I assume you are using bash in this instance?
If so, you should use shift.
An example:
Contents of shift.sh:
#!/bin/bash
shift 3
echo $*
Result:
graeme@graeme:~$ ./shift.sh one two three four five six
four five six
shift 2and leave off the args after "opt". – Dennis Williamson Apr 19 '10 at 16:00