Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

If I get a cmd line like:

myscript param1 param2 param3 param4 param5 param6 ... (so on)

How can I save a string consisting of parameters starting from some specific one? The $@ gives me the whole command line concatenated. What I need is to get a string starting like "param4 param5 param6 ... (so on)".

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

You have to take aways the uninteresting arguments by:

shift 3
share|improve this answer
add comment

You can use a variant on array slicing to do this:

args1to3="${*:1:3}"  # Three arguments starting from $1
args4on="${*:4}"     # The arguments starting from $4

BTW, this may not be what you want, because it just sticks the arguments together with spaces between them; if any of the arguments also contain spaces, it'll lose track of which spaces were inside arguments and which were between them (see BashFAQ #50). If you want to be able to keep them straight, use an array instead:

args1to3=("${@:1:3}")   # Three arguments starting from $1, as an array
args4on=("${@:4}")      # The arguments starting from $4, as an array

othercmd "${args1to3[@]}"             # Pass the first 3 arguments intact
for somearg in "${args4on[@]}"; do    # Process args 4 on, one at a time
  othercmd2 "$somearg"
done
share|improve this answer
add comment

Your Answer

 
discard

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.