Extract parameters before last parameter in "$@" Extract parameters before last parameter in "$@" bash bash

Extract parameters before last parameter in "$@"


To remove the last item from the array you could use something like this:

#!/bin/bashlength=$(($#-1))array=${@:1:$length}echo $array

Even shorter way:

array=${@:1:$#-1}

But arays are a Bashism, try avoid using them :(.


Portable and compact solutions

This is how I do in my scripts

last=${@:$#} # last parameter other=${*%${!#}} # all parameters except the last

EDIT
According to some comments (see below), this solution is more portable than others.
Please read Michael Dimmitt's commentary for an explanation of how it works.