4.2. Shell variables

4.2.1. Simple variables
4.2.2. Non volatile variables
4.2.3. Positional variables
4.2.4. Special variables
4.2.5. Accessing variables
4.2.6. Variables with special meanings

4.2.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.

4.2.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 (.).

Any changes to the non-volatile variables will not be made permanent until a nvsave command is issued.

A non-volatile variable my be “unset”, an unset parameter will use the system default value. A variable may be unset by assigning to an empty value or using the nvunset command.

The nvclear command can be used to restore the values back to the defaults. The nvsave should not be used to attempt to save these values, this would result in the current settings state being saved not the default values.

The value is interpreted differently depending on the settings data type, as with simple variables the value may need to be quoted to get a correct assignment.

Table 4.1. Non-volatile data types

Variable typeValues
Booleanon” and “off” (“true” or “false” and “0” or “1” may also be used).
NumericAny numeric value using only the digits between 0 and 9.
StringAny alphanumeric string.

In addition to simple assignment non-volatile variables may be changed with the nvset command. The syntax of the command is

nvset {variable} {value}

Where the variable is one of those shown in Appendix A, Non-Volatile Variables Reference and the value is correct for the variables type.

4.2.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 4.3, “Accessing positional parameters”

4.2.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 4.2. 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.


4.2.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 4.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 4.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
>

4.2.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 4.3. 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.4, “Filesystem navigation” for more details. This value is set to / by default.
OLDPWDThe previous working directory. Consult Section 3.4, “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.
PATHThe search path for commands. A series of directories to search for external commands. The directores are colon separated and may include the PWD which is represented by a single dot.
ABLE_VERSIONThe version of ABLE currently running. The major and minor version numbers are available seperately as ABLE_VERSION0 and ABLE_VERSION1 respectively.