Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
The way you're currently doing it right now looks fine. – tylerl Apr 19 '10 at 14:50
I'd use shift 2 and leave off the args after "opt". – Dennis Williamson Apr 19 '10 at 16:00
up vote 2 down vote accepted

Seems like a funky approach to arguments to me, but:

[kbrandt@kbrandt: ~/scrap] cat args
echo ${args[0]}
echo ${args[@]:1:2}
echo ${args[@]:0:$#}
[kbrandt@kbrandt: ~/scrap] bash args foo bar baz biz
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).

share|improve this answer
Thanks for the answer. Is there some sort of sub-array syntax, from 3 to max? Even from 3 to 9 would be enough for my needs. – Robert Munteanu Apr 19 '10 at 14:21
Updated accordingly (slice of array), not to sure about what the compatibility / portability of those slices is. – Kyle Brandt Apr 19 '10 at 14:25
Might be funky , indeed. I'm using it to force the first two arguments to always be fixed, and then the order of the rest is not enforced. – Robert Munteanu Apr 20 '10 at 6:33

I assume you are using bash in this instance?

If so, you should use shift.

An example:

Contents of

shift 3
echo $*


graeme@graeme:~$ ./ one two three four five six
four five six
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.