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
  • 51,350
Tim
  • 101,790

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 ...)
    
7
$ foo=(1 2 3 4)
$ echo "${foo[@]}"
1 2 3 4
$ echo "${foo[@]:0:2}"
1 2
echo "${foo[@]:2}"
3 4
DopeGhoti
  • 76,081