Variables

This subsection describes the following:

  • Variable substitution

  • Variable modifiers

  • Predefined shell variables

  • Formatting for the prompt variable

  • Sample .cshrc file

  • Environment variables

Variable Substitution

In the following substitutions, braces ({}) are optional, except when needed to separate a variable name from following characters that would otherwise be considered part of the name:

VariableDescription
${ var } The value of variable var.
${ var [ i ]}

Select word or words in position i of var. i can be a single number, a range m-n, a range -n (missing m implies 1), a range m- (missing n implies all remaining words), or * (select all words). i also can be a variable that expands to one of these values.

${# var } The number of words in var.
${#argv} The number of arguments.
$0 Name of the program.
${argv[ n ]}

Individual arguments on command line (positional parameters); 1 ≤ n ≤ 9.

${ n }

Same as ${argv[ n ]}.

${argv[*]} All arguments on command line.
$* Same as {$argv[*]}.
$argv[$#argv] The last argument.
${? var }

Return 1 if var is set, 0 if var is not set.

$$

Process number of current shell; useful as part of a filename for creating temporary files with unique names.

${?name} Return 1 if name is set, 0 if not.
$?0 Return 1 if input filename is known, 0 if not.

Examples

Sort the third through last arguments and save the output in a file whose name is unique to this process:

                  sort $argv[3-] > tmp.$$

Process .cshrc commands only if the shell is interactive (i.e., ...

Get Linux in a Nutshell, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.