Hamilton LaboratoriesHamilton C shell 2012User guideLanguage reference

@, calc, set, setenv and local

Oregon Coast

@, calc, set, setenv and local
Previous | Next

Topics

Syntax
Usage
Local variables
See also

Syntax

@ expr calc expr set variable-or-array-element [ = ] ( wordlist ) set variable-or-array-element [ = ] wordlist setenv environment-variable [ = ] ( wordlist ) setenv environment-variable [ = ] wordlist shift [ variable ] set set variable set pattern setenv setenv variable setenv pattern unset list-of-variables unset pattern unsetenv list-of-environment-variables unsetenv pattern local list-of-variables local local pattern unlocal list-of-variables unlocal pattern

Usage

The @ and calc statements will each calculate the value of an expression; the @ statement does it silently while the calc statement writes the result to stdout.

The set, setenv and shift statements manipulate variables as words rather than expressions. set defines a variable that's shared between all threads in the shell; setenv puts it into the environment which is inherited by child processes. A named_ref can be either a simple variable name or an indexed variable, e.g., files[2].

set or setenv with no operands prints a list of all defined variables of that type. set name or setenv name with no additional arguments prints the value of the named variable. set pattern or setenv pattern prints the values of all the variables whose names match the pattern. To resolve the ambiguity between a pattern and an indexed variable reference, pattern arguments to set or setenv cannot use the [ as the first wildcard character in the string.

unset namelist or unsetenv namelist discard the specified variables. unset pattern or unsetenv pattern discard all the shell or environment variables, respectively, that match the pattern.

By convention on Windows, environment variable names are case-insensitive, so any patterns used with setenv or unsetenv are also considered case-insensitive.

Local variables

The local command lets you define a list of variable names that you don't to share with other routines or other processes or threads (except your own child threads). When you define a local variable it hides any previous definition from any outer statement list. (But you are not permitted to redefine any of the builtin set or setenv variable names.)

The namelist may be typed with or without commas between the names. When you create a new local variable, its initial value is always a null string. local with no operands reports the currently defined and accessible local variables, if any. local pattern lists all the local variables whose names match the pattern.

When you spawn a child thread either implicitly, e.g., to run the second or following stage of a pipeline or explicitly, by typing & at the end of a command to run it in the background all your current local variables are snapshotted and copied to the child. If, following that, either the parent or the child changes the value of any of these local variables, it affects only its own copy.

Local variables are automatically discarded as soon as execution leaves the statement nesting level in which the variable was created. You can also explicitly discard local variables using the unlocal command.

unlocal namelist discards the specified local variables. unlocal pattern discards all those whose names match the pattern.

In all other respects, local variables act just like any other variables, though you may find they're slightly faster since the shell doesn't need to semaphore its use of them.

See also

Predefined variables
Variable substitution
Expression operators
Wildcard characters
Tutorial: Variables
Tutorial: Expressions
Tutorial: Wildcarding

Previous | Next

Getting started with Hamilton C shell

Hamilton C shell, as it first wakes up.

Getting started with Hamilton C shell

A first few commands.

You can set the screen colors to your taste.

You can set the screen colors to your taste.