7

($@) 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 $@
l0b0
  • 50,672
  • 41
  • 197
  • 360
Tim
  • 98,580
  • 191
  • 570
  • 977

2 Answers2

13

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 ...)
    
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
7
$ foo=(1 2 3 4)
$ echo "${foo[@]}"
1 2 3 4
$ echo "${foo[@]:0:2}"
1 2
echo "${foo[@]:2}"
3 4
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
DopeGhoti
  • 73,792
  • 8
  • 97
  • 133