On Unix-like operating systems, the set command is a built-in function of the Bourne shell (sh), C shell (csh), and Korn shell (ksh), which is used to define and determine the values of the system environment.
Syntax
Bourne shell (sh):
- Syntax
- Examples
- Related commands
- Linux commands help
set [–aefhkntuvx[argument]]…
C shell (csh):
set [var[=value]]
set var [n] = word
Korn shell (ksh):
set [+-aefhkmnopstuvx] [+-o option]… [+-A name] [arg…]
Options: Bourne Shell (sh)
In sh, the set built-in command has the following options:
Using + rather than - causes these flags to be turned off. These flags can also be used upon invocation of the shell itself. The current set of flags are found in the variable $-. The remaining arguments are positional parameters and are assigned, in order, to $1, $2, etc. If no arguments are given the values of all names are printed.
For each name, the unset command removes the corresponding variable or function value. The special variables PATH, PS1, PS2, MAILCHECK, and IF cannot be unset.
With the export built-in command, the given names are marked for automatic export to the environment of subsequently executed commands. If no arguments are given, variable names that are marked for export during the current shell’s execution are listed. Function names are not exported.
Options: C Shell (csh)
In csh, If no arguments are specified, set displays the values of all shell variables. Multiword values are displayed as a parenthesized list. With the var argument alone, set assigns an empty (null) value to the variable var. With arguments of the form var = value, set assigns value to var, where value is one of:
Values are command and file name expanded before being assigned. The form set var[n]=word replaces the n’th word in a multiword value with word.
unset removes variables whose names match (using file name substitution) pattern. All variables are removed by “unset *”; this is a very bad idea if you don’t know what you’re doing, though.
Options: K Shell (ksh)
In ksh, the set command takes the following options:
Using + rather than - causes these flags to be turned off. These flags can also be used upon invocation of the shell. The current set of flags are found in $-. Unless -A is specified, the remaining arguments are positional parameters and are assigned, in order, to “$1 $2 …”. If no arguments are given, then the names and values of all variables are printed on the standard output.
The variables given by the list of names are automatically unassigned: their values and attributes are erased. Readonly variables cannot be unset. If the -f flag is set, then the names refer to function names. Unsetting ERRNO, LINENO, MAILCHECK, OPTARG, OPTIND, RANDOM, SECONDS, TMOUT, and _ removes their special meaning even if they are subsequently assigned.
When using unset, the variables given by the list of names are similarly unassigned: their values and attributes are erased. Readonly variables cannot be unset. If the -f, flag is set, then the names refer to function names. Unsetting ERRNO, LINENO, MAILCHECK, OPTARG, OPTIND, RANDOM, SECONDS, TMOUT, and _ removes their special meaning even if they are subsequently assigned.
With the export built-in command, the given names are marked for automatic export to the environment of subsequently-executed commands. ksh commands that are preceded by one or two “*” characters (asterisks) are treated specially in the following ways:
- Variable assignment lists preceding the command remain in effect when the command completes.
- I/O redirections are processed after variable assignments.
- Errors cause a script containing them to abort.
- Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a variable assignment. This assignment means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed.
Examples
set PATH="/bin:/usr/bin:/usr/sbin:usr/local/bin"
In csh, this command sets the environment variable PATH, such that the shell searches for files in the /bin, /usr/bin, /usr/sbin and /usr/local/bin directories, in that order.
Related commands
csh — The C shell command interpreter.ksh — The Korn shell command interpreter.setenv — Set the value of an environment variable.sh — The Bourne shell command interpreter.