Up Next Previous

## Variable substitution

The shell maintains a list of variables, each of which has as value a list of zero or more words. The values of shell variables can be displayed and changed with the set and unset commands. The system maintains its own list of ``environment'' variables. These can be displayed and changed with printenv, setenv and unsetenv.

(+) Variables may be made read-only with `set -r' (q.v.). Read-only variables may not be modified or unset; attempting to do so will cause an error. Once made read-only, a variable cannot be made writable, so `set -r' should be used with caution. Environment variables cannot be made read-only.

Some variables are set by the shell or referred to by it. For instance, the argv variable is an image of the shell's argument list, and words of this variable's value are referred to in special ways. Some of the variables referred to by the shell are toggles; the shell does not care what their value is, only whether they are set or not. For instance, the verbose variable is a toggle which causes command input to be echoed. The -v command line option sets this variable. Special shell variables lists all variables which are referred to by the shell.

Other operations treat variables numerically. The `@' command permits numeric calculations to be performed and the result assigned to a variable. Variable values are, however, always represented as (zero or more) strings. For the purposes of numeric operations, the null string is considered to be zero, and the second and subsequent words of multi-word values are ignored.

After the input line is aliased and parsed, and before each command is executed, variable substitution is performed keyed by `$' characters. This expansion can be prevented by preceding the `$' with a `\' except within `"'s where it always occurs, and within `''s where it never occurs. Strings quoted by ``' are interpreted later (see Command substitution below) so `$' substitution does not occur there until later, if at all. A `$' is passed unchanged if followed by a blank, tab, or end-of-line.

Input/output redirections are recognized before variable expansion, and are variable expanded separately. Otherwise, the command name and entire argument list are expanded together. It is thus possible for the first (command) word (to this point) to generate more than one word, the first of which becomes the command name, and the rest of which become arguments.

Unless enclosed in `"' or given the `:q' modifier the results of variable substitution may eventually be command and filename substituted. Within `"', a variable whose value consists of multiple words expands to a (portion of a) single word, with the words of the variable's value separated by blanks. When the `:q' modifier is applied to a substitution the variable will expand to multiple words with each word separated by a blank and quoted to prevent later command or filename substitution.

The following metasequences are provided for introducing variable values into the shell input. Except as noted, it is an error to reference a variable which is not set.

$name

${name}
  Substitutes the words of the value of variable name, each separated by a blank. Braces insulate name from following characters which would otherwise be part of it. Shell variables have names consisting of letters and digits starting with a letter. The underscore character is considered a letter. If name is not a shell variable, but is set in the environment, then that value is returned (but some of the other forms given below are not available in this case).

$name[selector]

${name[selector]}
  Substitutes only the selected words from the value of name. The selector is subjected to `$' substitution and may consist of a single number or two numbers separated by a `-'. The first word of a variable's value is numbered `1'. If the first number of a range is omitted it defaults to `1'. If the last member of a range is omitted it defaults to `$#name'. The selector `*' selects all words. It is not an error for a range to be empty if the second argument is omitted or in range.

$0
  Substitutes the name of the file from which command input is being read. An error occurs if the name is not known.

$number

${number}
  Equivalent to `$argv[number]'.

$*
  Equivalent to `$argv', which is equivalent to `$argv[*]'.

The `:' modifiers described under History substitution, except for `:p', can be applied to the substitutions above. More than one may be used. (+) Braces may be needed to insulate a variable substitution from a literal colon just as with History substitution (q.v.); any modifiers must appear within the braces.

The following substitutions can not be modified with `:' modifiers.

$?name

${?name}
  Substitutes the string `1' if name is set, `0' if it is not.

$?0
  Substitutes `1' if the current input filename is known, `0' if it is not. Always `0' in interactive shells.

$#name

${#name}
  Substitutes the number of words in name.

$#
  Equivalent to `$#argv'. (+)

$%name

${%name}
  Substitutes the number of characters in name. (+)

$%number

${%number}
  Substitutes the number of characters in $argv[number]. (+)

$?
  Equivalent to `$status'. (+)

$$
  Substitutes the (decimal) process number of the (parent) shell.

$!
  Substitutes the (decimal) process number of the last background process started by this shell. (+)

$_
  Substitutes the command line of the last command executed. (+)

$<
  Substitutes a line from the standard input, with no further interpretation thereafter. It can be used to read from the keyboard in a shell script. (+) While csh always quotes $<, as if it were equivalent to `$<:q', tcsh does not. Furthermore, when tcsh is waiting for a line to be typed the user may type an interrupt to interrupt the sequence into which the line is to be substituted, but csh does not allow this.

The editor command expand-variables, normally bound to `^X-$', can be used to interactively expand individual variables.

Up Next Previous