5

($@) Expands to the positional parameters, starting from one.

How can I get the positional parameters, starting from two, or more generally, n?

I want to use the positional parameters starting from two, as arguments to a command, for example,

myCommand $@
  • Do you want to preserve 1 to n positional parameters? – cuonglm Mar 16 '17 at 15:48
  • I am not sure. Can I ask for both cases? – Tim Mar 16 '17 at 15:50
  • as an array? as $@ but shifted n ? – Jeff Schaller Mar 16 '17 at 15:50
  • @Jeff: if used as an array, will need shifting so that the first entry in the array is the nth position parameter. It is possible they are not used as an array, – Tim Mar 16 '17 at 15:53
  • 1
    Note that it almost never makes sense to leave $@ unquoted. – Stéphane Chazelas Mar 16 '17 at 17:29
11

For positional parameters starting from the 5th one:

  • zsh or yash.

    myCommand "${@[5,-1]}"
    

    (note, as always, that the quotes above are important, or otherwise each element would be subject to split+glob in yash, or the empty elements removed in zsh).

  • ksh93, bash or zsh:

    myCommand "${@:5}"
    

    (again, quotes important)

  • Bourne-like shells (includes all of the above shells)

    (shift 4; myCommand "$@")
    

    (using a subshell so the shift only happens there).

  • csh-like shells:

    (shift 4; myCommand $argv:q)
    

    (subshell)

  • fish:

    myCommand $argv[5..-1]
    
  • rc:

    @{shift 4; myCommand $*}
    

    (subshell)

  • rc/es:

    myCommand $*(`{seq 5 $#*})
    
  • es:

    myCommand $*(5 ...)
    
|improve this answer|||||
6
$ foo=(1 2 3 4)
$ echo "${foo[@]}"
1 2 3 4
$ echo "${foo[@]:0:2}"
1 2
echo "${foo[@]:2}"
3 4
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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