Chapter 3: Basic Shell Features

19

(see Section 3.5.5 [Arithmetic Expansion], page 29). Word splitting is not performed, with the exception of "$@" as explained below. Filename expansion is not performed. Assign- ment statements may also appear as arguments to the alias, declare, typeset, export, readonly, and local builtin commands. When in posix mode (see Section 6.11 [Bash

POSIX Mode], page 94), these builtins may appear in a command after one or more in- stances of the command builtin and retain these assignment statement properties.

In the context where an assignment statement is assigning a value to a shell variable or array index (see Section 6.7 [Arrays], page 89), the ‘+=’ operator can be used to append to or add to the variable’s previous value. When ‘+=’ is applied to a variable for which the integer attribute has been set, value is evaluated as an arithmetic expression and added to the variable’s current value, which is also evaluated. When ‘+=’ is applied to an array variable using compound assignment (see Section 6.7 [Arrays], page 89), the variable’s value is not unset (as it is when using ‘=’), and new values are appended to the array beginning at one greater than the array’s maximum index (for indexed arrays), or added as additional key-value pairs in an associative array. When applied to a string-valued variable, value is expanded and appended to the variable’s value.

A variable can be assigned the nameref attribute using the -n option to the \fBde- clare\fP or \fBlocal\fP builtin commands (see Section 4.2 [Bash Builtins], page 48) to create a nameref, or a reference to another variable. This allows variables to be manipu- lated indirectly. Whenever the nameref variable is referenced or assigned to, the operation is actually performed on the variable specified by the nameref variable’s value. A nameref is commonly used within shell functions to refer to a variable whose name is passed as an argument to the function. For instance, if a variable name is passed to a shell function as its first argument, running declare -n ref=$1 inside the function creates a nameref variable ref whose value is the variable name passed as the first argument. References and assignments to ref are treated as references and assignments to the variable whose name was passed as $1.

If the control variable in a for loop has the nameref attribute, the list of words can be a list of shell variables, and a name reference will be established for each word in the list, in turn, when the loop is executed. Array variables cannot be given the -n attribute.

However, nameref variables can reference array variables and subscripted array variables.

Namerefs can be unset using the -n option to the unset builtin (see Section 4.1 [Bourne

Shell Builtins], page 41). Otherwise, if unset is executed with the name of a nameref variable as an argument, the variable referenced by the nameref variable will be unset.

3.4.1 Positional Parameters

A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell’s arguments when it is invoked, and may be reassigned using the set builtin command. Positional parameter N may be referenced as ${N}, or as $N when N consists of a single digit. Positional parameters may not be assigned to with assignment statements. The set and shift builtins are used to set and unset them (see Chapter 4 [Shell Builtin Commands], page 41). The positional parameters are temporarily replaced when a shell function is executed (see Section 3.3

[Shell Functions], page 17).