3.5. Shell variables

3.5.1. Simple variables
3.5.2. Non volatile variables
3.5.3. Positional variables
3.5.4. Special variables
3.5.5. Accessing variables
3.5.6. Variables with special meanings

3.5.1. Simple variables

The ABLE shell supports simple variables, these variables are in the form:

name= [value]

where the name must start with a letter of the alphabet (upper or lower case) or an underscore (_) and continue with letters of the alphabet, numbers or an underscore (_). The value may contain any arbitrary numeric or textural value and when omitted is a null (empty) string. The value may need to be quoted in order to get a correct assignment.

3.5.2. Non volatile variables

The non-volatile variables are also accessible from within the shell. The variable form is identical to that of simple variables with one exception, the body of the variable name will contain a full stop (.).

The value is interpreted in the same way as if passed to the nvset command. Options that take boolean values can be set with “on” and “off” (“true” or “false” and “0” or “1” may also be used). Other options typically take free form text, as with simple variables the value may need to be quoted to get a correct assignment.

Any changes to the non-volatile variables will not be made permanent until a nvsave command is issued. Chapter 4, Setting Options contains details on using the non-volatile settings.

3.5.3. Positional variables

The positional parameters are similar to the simple variables except the variable name is a positive integer number. Each parameter is set from the arguments to the shell or script when it was started.

The single digit 0 has special meaning and is set to the name of the shell or script.

Positional parameters cannot be assigned with the normal assignment operator and are read only.

When the tenth or later positional parameters are referenced they must be disambiguated using curly braces this is shown fully in Example 3.3, “Accessing positional parameters”

3.5.4. Special variables

In addition to simple variables there are a small number of “special” variables which do not match the syntax for simple variable names. These typically access specific information within the shell and are read only.

Table 3.1. Special variables

Variable nameValue
?Exit status of last command
-Shell parameters
*All the positional parameters separated by the first character of the IFS variable.
#The number of positional parameters.


3.5.5. Accessing variables

Variables are accessed within the shell by using the dollar symbol followed by the variable name $name. The variable name my also be surrounded by curly braces ${name}.

The curly brace form is less ambiguous because if the braces are omitted the shell may not be able to distinguish between a variable name and the text surrounding it. The curly brace form is unambiguous as the variable name is clearly delimited.

Example 3.2. Using curly braces to disambiguate variables

This example shows a variable myvariable being set and then displayed using the two forms showing the ambiguity problem.

>myvariable=hello 
>echo foo $myvariable bar 
foo hello bar 
>echo foo$myvariablebar 
foo 
>echo foo${myvariable}bar 
foohellobar 
>

Special care must be taken of positional parameters, the simple version with no braces is limited to the nine single digits 1-9 to access the the later positional parameters curly braces must be used.

Example 3.3. Accessing positional parameters

This example shows accessing the first positional parameter, then the first positional parameter when the tenth was meant and finally accessing the tenth parameter correctly.

>echo $1
one
>echo $10
one0
>echo ${10}
ten
>

3.5.6. Variables with special meanings

There are a small number of shell variables which have special meaning to the shell environment itself. These variables generally affect some aspect of the shell environment. Several of these variables have defaults which are assigned by the shell upon initialisation.

Table 3.2. Variables with special meanings

VariableMeaning
PS1This variable contains the text of the prompt to use, which allows users to alter their prompt or scripts to use the same prompt as the shell. This value is set to > by default.
PS4This variable is the forth level prompt, it is used when the -x switch is in operation to preface output lines. This value is set to + by default.
PWDThis variable is the present working directory within the filesystem. Consult Section 3.6, “Filesystem navigation” for more details. This value is set to / by default.
OLDPWDThe previous working directory. Consult Section 3.6, “Filesystem navigation” for more details.
IFSThe Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read command. This value is set to “space tab newline” by default.