Table of Contents

PARAMETERS

Parameter Index

A parameter has a name, a value, and a number of attributes. A name may be any sequence of alphanumeric characters and _'s, or the single characters *, @, #, ?, -, $, or !. The value may be either a scalar (a string), an integer, or an array. To assign a scalar or integer value to a parameter, use the typeset builtin. To assign an array value, use

set -A name value ....

The value of a parameter may also be assigned by writing:

name=value ...

If the integer attribute, -i, is set for name, the value is subject to arithmetic evaluation.

The value of an array parameter may be assigned by writing:

name=(value ...) ...
Individual elements of an array may be selected using a subscript. A subscript of the form [exp] selects the single element exp, where exp is an arithmetic expression which will be subject to arithmetic expansion as if it were surrounded by "$[...]". The elements are numbered beginning with 1. A subscript of the form [*] or [@] evaluates to all elements of an array; there is no difference between the two except when they appear within double quotes. "$foo[*]" evaluates to "$foo[1] $foo[2] ...", while "$foo[@]" evaluates to "$foo[1]" "$foo[2]", etc. A subscript of the form [exp1,exp2] selects all elements in the range exp1 to exp2, inclusive. If one of the subscripts evaluates to a negative number, say - n, then the nth element from the end of the array is used. Thus "$foo[-3]" is the third element from the end of the array foo, and "$foo[1,-1]" is the same as "$foo[*]".

Subscripting may also be performed on non-array values, in which case the subscripts specify a substring to be extracted. For example, if FOO is set to foobar, then echo $FOO[2,5] prints ooba.

If a subscript is used on the left side of an assignment the selected range is replaced by the expression on the right side.

If the opening bracket or the comma is directly followed by an opening parentheses the string up to the matching closing one is considered to be a list of flags. The flags currently understood are:

e
the argument is expanded using full shell expansion first

w
if the parameter subscripted is a scalar than this flag makes subscription work on a per-word basis instead of characters

s:string:
this gives the string that separates words (for use with the w flag)

r
if this flag is given the exp is taken as a pattern and the result is the first matching array element, substring or word (if the parameter is an array, if it is a scalar, or if it is a scalar and the w flag is given, respectively); note that this is like giving a number: $foo[(r)??,3] and $foo[(r)??,(r)f*] work

R
like r, but gives the last match

i
like r, but gives the index of the match instead; this may not be combined with a second argument

I
like i, but gives the index of the last match

n:expr:
if combined with r, R, , or I, makes them give the n'th or n'th last match (if expr evaluates to n)

Positional Parameters

Positional parameters are set by the shell on invocation, by the set builtin, or by direct assignment. The parameter n, where n is a number, is the nth positional parameter. The parameters *, @, and argv are arrays containing all the positional parameters; thus argv[n], etc. is equivalent to simply n.


Mark D. Borges