A compilation of documentation   { en , fr }

Easy access to the arguments of a bash script

Tags:
Created on:
Last edited on:
Author:
Xavier Béguin

Caution: the functionalities described here are specific to the bash interpreter, and are not supported in other interpreters, especially those that only implement functionalities defined in the POSIX norm (or when bash is running in PODIX mode).

Accessing a positional parameter indexed on the last parameter

In a shell script or function, we often need to access an argument that was passed to it. It's fairly easy when we know its index in the list of arguments, thanks to the variables $1, $2, ...

It is however also possible to access a parameter of which we know the index starting from the end of the list of parameters :

These parameters are however also accessibles through the arrays $@ et $*. Thanks to the functionalities specific to the bash interpreter, it is possible, among other things, to access a paramter of which we know the index starting from the end of the list of parameters :

bash$ function f() {
    echo "my second to last parameter is '${*: -2:1}'"
}
bash$ f on two three
my second to last parameter is 'two'"

Caution: do not forget to insert a space between the colon and the dash, to avoid confusing bash with the ${variable:-word} notation that represents the use of default values.

Other similar possibilities

Reading arguments using character strings

Below are some examples where we retrieve a character string holding one or more positional parameters (separated by the first parameter of the IFS variable, or a space if it is unset, as it is always the case within double quotes for arrays subscripted by * or for $*):

  • here we initialize and print the character string s that will contain the last positional parameter of the function f:
    bash$ function f() { s="${*: -1}"; echo "$s"; }
    bash$ f one two three
    three
    
  • below, the string s will hold the last two parameters (sepated by a space):
    bash$ function f() { s="${*: -2:2}"; echo "$s"; }
    bash$ f one two three four
    two three
    

Reading arguments using arrays

Below are a few examples where we initialize an array holding one or more positional parameters (resulting from the usual expansion that takes place within double quotes for arrays subscripted by @, or with $@) before printing their elements (separated by a space):

  • below, t represents an array with 2 positional parameters starting from the second parameter of the function, i.e. the same as t=("$2" "$3"):
bash$ function f() { t=("${@:2:2}"); echo "${t[*]}"; }
bash$ f one two three four
two three
  • here, t is an array holding two consecutive parameters, whose first one is the third positional parameter of the function, counting backwards from the last parameter:
bash$ function f() { t=("${@: -3:2}"); echo "${t[*]}"; }
bash$ f one two three four five
three four
  • lastly, in this example, t will hold the five last positional parameters of the function (the array will be empty if there is less than 5 parameters to the function):
bash$ function f() { t=("${@: -5}"); echo "${t[*]}"; }
bash$ f one two three

bash$ f one two three four five six seven
three four five six seven