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

Hennes
  • 64,768
  • 7
  • 111
  • 168
azerIO
  • 385
  • 1
  • 4
  • 14

2 Answers2

4

You have to take aways the uninteresting arguments by:

shift 3
ceving
  • 1,935
  • 3
  • 21
  • 27
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
Gordon Davisson
  • 34,084
  • 5
  • 66
  • 70