Bash Parameters – How to Slice Positional Parameters

arraybashparameter

How can I get a slice of $@ in Bash without first having to copy all positional parameters to another array like this?

argv=( "$@" )
echo "${argv[@]:2}";

Best Answer

You can use the same format as for any other array. To extract the 2nd and 3rd elements from $@, you would do:

echo "${@:1:2}"
          - -
          | |----> slice length
          |------> slice starting index 
Related Question