4

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)".

2 Answers 2

4

You have to take aways the uninteresting arguments by:

shift 3
4

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .