Shell – How to get the positional parameters, starting from two, or more generally, `n`

arrayshellvariable

($@) 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 $@

Best Answer

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