Go to the first, previous, next, last section, table of contents.


Variables

Variables are names which can have values associated with them. The name can be any combination of alphabetic, numeric or underscore characters, providing that the first character is not numeric. The values can be a character string, an integer, or an array of other values. See the Builtin vartype.

Character String Variables

Most variables are characters strings. Such a string can contain any character other than the nul character. When a variable is first used, it is assumed to be a string variable unless it is used in a context that specifically requires an integer variable.

Integer Variables

An integer variable can hold a signed value, the range of which is machine specific but usually such that it fits into a 32 bit word. That is approximately plus or minus two thousand million.

When assigning a string to an integer variable, the string gets processed by the arithmetic unit to convert it to an integer. "' what happens on error FIXME Thus assigning the string "4+5" to an integer variable will place the value `9' in it, whereas assigning the same string to a string variable will place the value "4+5" in it.

When a variable name appears in a string that is being processed by the arithmetic unit, the named variable is automatically converted into an integer variable. If it was not already an integer variable, its value will first be converted to a string (if necessary) and then converted to an integer by a recursive call to the arithmetic unit.

Array Variables

An array variable contains an arbitrary list of other variables. These variables can be integers, strings or other arrays. There is no need for every element of an array to be the same type. Array elements can also be undefined. Indeed an array could be considered to be an infinite list of variables, each of which is one of string, integer, array or undefined, providing that there is only a finite number of non-undefined variables.

Each array has, associated with it, a separator character. This provides information on how to convert a string to an array, and an array to a sting. When an array variable has a string assigned to it, the string is broken in to pieces based on the separator character. If the separator character is undefined, then the string is broken up using the "Blank Substitution" algorithm based on the IFS (Input Field Separator) variable. If the separator character is defined, then instances of that character in the string are taken to be break points. Two adjacent separator characters will create an empty field in the break up. Once the string has been broken up, the piece are assigned individually to consecutive elements of the array. All entries in the array after the last one assigned to become undefined.

When assigning to an element of an array that is undefined, the type is taken to be the same as the type of element zero in the array.

When a array in to be converted to a string, each element is converted to a string, and these strings are joined by single instances of the separator character. If there is no separator character, the first character of IFS is used. If there is no such first character, a space is used.

Arrays are indexed by integers from zero up. For most purposed, though, only the entries from 1 up are used. Thus when converting an array to a string, the "zeroth" entry is not included, unless specifically asked for. Similarly when assigning to an array, the "zeroth" entry is left unchanged.

It is possible to have a temporary array for which the separator character is defined to a non-character value. This is known as a "hard" array. When such an array is assigned to, the string must have been generated from expanding a similar hard array, and the individual entries in the second array are assigned to those of the first array without joining them together and then splitting them apart. When such an array is substituted into a wordlist, the elements of the array become individual words, no matter how the whole array substitution was quoted. Also, as mentioned above, value modifiers in variable substitution are applied to every element of a hard array individually.

Elements of an array are accessed by following the name of the array with and index surrounded by brackets. The index can take one of several forms. It can be an arithmetic expression as in

array[4+5]

which will provide the appropriate element of the array. It can be two expressions separated by a colon as in

array[2:20]

which will provide a new (temporary) sub-array containing the elements in the range. Note that this temporary array is indexed from one, so the following are equivalent:

array[2:20][4]
array[5]

It can be an expression followed by an asterisk, such as

array[x*]

which provides a sub-array containing the elements from the given number onwards. Or it can be an expression followed by an at sign:

array[3@]

which provides a sub-array similar to where the asterisk is used, except that it is a hard array. If the number would be one, it can be omitted, thus

array[1*]
array[*]
array

are all equivalent.

For ease of access to the argument list, and for backward compatibility, the ARGV array can be accessed by just naming the index. This index must use literal decimal numbers as expressions, and cannot use the range index. Thus the following pairs are equivalent:

$2	${ARGV[2]}
${0}	${ARGV[0]}
${3*}	${ARGV[3*]}

It should be noted that access array elements in Variable Substitution requires the use of braces. The expression

echo $array[4]

will not give the desired result.

Local Variables

Reserved Variable Names

The following variables have special meaning to the shell. so as to reduce confusion, they can not have their types changed from that described here.

PS1
This string variable is use as the primary prompt. This prompt is displayed in interactive mode when the shell is waiting to read another command. Any `!' not preceded by a slosh is replaced with the number of the current command. Any `?' is replaced by the value of the `?' parameter, unless that is 0, in which case the `?' is discarded. The default value is "%~" for normal users, and "#~" for the super user.
PS2
This string variable is used as the secondary prompt. This prompt is printed when another line of a multi-line command is needed. The same `!' and `?' substitutions apply as with PS1. The default value is ">~".
INTERRUPT
This string variable is printed whenever the shell receives the INTERRUPT signal, or when a child process dies from that signal. Default value is "Interrupt" preceded by a newline.
HOME
This string variable names a directory which is considered to be the home directory. It is used by the cd (change directory) command as a default. Though the shell provides no default, this variable is usually defined in the environment created by the login program.
PATH
This variable is an array of strings with the colon (:) as its separator character. It should contain a list of directory names. When a command is to be executed, each directory in this list is search for an executable file with a name matching the command. See Execution for further details. The default value is "/bin:".
CDPATH
Similar to PATH but used by cd when looking for a named directory. There is no default value. This value should be an array with colon as the separator, but currently it isn't. I should fix this.
IFS
This string variable contains a list of characters (the Input Field Separators) which are the word separators used in Blank Substitution. The first of these characters is used when the shell needs a separator character as when expanding the `*' parameter. It is also used for array variables that have no separator character. Default value is space, newline, tab.
SHELL
This string should be set to the path name of `ae'. It is the program use to execute a shell script. See Execution. The default value is "/bin/sh". It must be the full pathname of an executable program.
HISTORY
This numeric variable holds the maximum number of commands that the shell will remember in its history. Any value less than 1 is equivalent to 1. The default value is ..... 100?
MAIL
If this contains the name of a file, and no action has been specified for the PARSE trap, the shell will provide notification whenever the size of that file increases. The message is "You have mail."
NOTIFY
This string variable contains a list of characters that indicate what sorts of changes in job status should cause notification. Different letters refer to different state changes, with lower case referring to foreground jobs, and upper case referring to background jobs. The letters are:
`x'
Commence Execution.
`s'
Job is suspended.
`r'
Job is restarted after being suspended.
`e'
Job exits.
`k'
Job is killed.
The default value is "XsSRrEkK".
MATCH
This variable controls File Name Expansion (described below). If the first character is `N' then file name expansion is disabled (None). If it is `E' then any word containing a pattern character must match at least one file name, or cause an error (Exact). If the first character is `P' then any word with pattern characters will expand to precisely those file names it matches, even if that is an empty list (Precise). Any other leading character has the effect of `P' if there are any matches, otherwise the effect of `N'.
SHELLRC
If this variable is set in the environment on execution of the shell, it determines what start-up commands should be performed by the shell if it is being used interactively. If the value of this variable starts with a slash (/) then it is taken to be the name of a file to read commands from, otherwise it is assumed to contain the actual commands.
TIMEOUT
This variable controls idle timeout. It is an array of strings with the colon (:) as the separator. Each entry is a time in minutes and seconds separated by a period, followed by a message. If the shell waits after issuing the primary prompt for more than the time given in the first entry, it issues the associated message and then goes to the next entry and waits some more. When it runs out of entries, it exits. An entry giving a time of 0 will cause an infinite wait thus disabling timeout. The default value will give a 30 second warning after 10 minutes of idle time. TIMEOUT doesn't currently work. I should look at it someday.
IGNOREEOF
If set (to any value) and the shell is interactive, an End Of File on input will not cause the shell to exit. If, however, the shell receives ten consecutive end of file indications, it will exit regardless of the setting of this variable.
VERSION
This string variable is always made read-only, and is set to a string indicating which version of the shell is running. The string contains seven `words' separated by spaces. The first two identify the shell by name and version number (currently ash 2.0.54). The second two identify the host type and operating system that ash was compiled to run under, for example Apollo SR10. The last three give the date of the last change, as month, date and year. e.g. May 16 1991
DIGITS
This string variable is used for converting integers into string (though not currently for converting strings into integers). It can be set to a sequence of character which should be used to represent the digits from 0 upwards. e.g. to get hexadecimal numbers to print in uppercase, use
DIGITS=0123456789ABCDEF
The default is to use lower case, and only to allow bases up to 36.
ARGV
This array of strings is used to store the arguments passed to a shell script or shell function. When a function (or alias) is called, a new local instance of this variable is created, and the arguments are placed in it.
WHICH
The first three characters of this string variable control the searching of paths. The correspond to searching PATH for an executable file, searching CDPATH for a directory, or searching PATH for any file (as with the . builtin). Each of these three characters should be one of S,D,N,A,s,d,n,a. They determine when path searching should actually be done, and when the file name given should just be used as is. If it is appropriate to the the file name as is, and that name doesn't correspond to the appropriate type of object, then the path is tried if the letter was uppercase, and the search fails immediately if the letter was lowercase. The letters stand for
`S,s'
Try the name by itself if it contains a slash.
`D,d'
Try the name by itself if it starts with on of /, ./, ../.
`N,n'
Never try the name by itself.
`A,a'
Always try the name by itself.
The default value is "dDd", though for compatibility with the Bourne shell, it should probably be "sSs".


Go to the first, previous, next, last section, table of contents.