Previous Table of Contents Next


Module 118
set/unset

DESCRIPTION

The internal set command is used to control the current shell environment. It can perform three basic functions:

*  Set options to control how the shell functions.
*  Set positional parameters.
*  List local environment; the name and value of variables.

C Shell
The C shell utilizes the set command differently from the Bourne or Korn shells. It assigns a value to a variable and displays the name and value of all variables.

The internal unset command removes variables and functions from the shell environment. It does not set them to NUL (\0) but removes the name from the variable table of the shell. Setting a variable to NUL leaves the variable name in the shell variable table with a value of NUL.

COMMAND FORMAT

Following is the general format of the set command.

     set [ -aefhkmnopstuvx ] [ -o opt_arg ] [ -A name ] [ args ]
     set [ +aefhkmnopstuvx ] [ +o opt_arg ] [ +A name ] [ args ]

     unset [ variables ]
     unset [ -f ] [ functions ]

C Shell

     set variable
     set variable=value
     set variable=variable[index]
     set variable=(wordlist)
     set variable=$<

     unset variable

Options

The following list describes the options and their arguments that may be used with the set command.

-- If arguments follow, do not interpret as options. Prevents a -opt in $1 from being interpreted as an option. If no arguments follow then unset all positional parameters.
-a Automatically export all variables as they are assigned values. Implies that an export variableis performed after each variable is assigned a value.
-e Exit the shell immediately if running a shell script and an error occurs.
-f Filename generation is disabled. The shell no longer expands wildcard characters.
-h Causes each command that you enter on the command line to become a tracked alias.
-k Place all keyword arguments in the environment for a command. By default, only the keyword arguments preceding the command are exported to the environment.
-m Enable the job monitor. A line will be displayed upon completion of each background job. The exit status of each background job is displayed with the completion message.
-n Read but do not execute commands. Useful to check for syntax errors in shell scripts.
-o optargs    Allows you to set other and special options.
allexport Same as -a
bgnice Run all background jobs at a lower priority.
emacs The emacs style in-line editor is used for command reentry. See In-line Command Editing in the ksh command, Module 71.
errexit Same as -e
gmacs The gmacs style in-line editor is used for command reentry.
ignoreeof If the shell is the login shell, it will not exit on end-of-file (Ctrl-D). The exit command must be entered.
keyword Same as -k
markdirs A / is appended to all directory names resulting from pathname expansion.
mapesc value Map the escape character (033) to a new value.
noclobber Redirection will not overwrite an existing file. You must use the >| symbol to overwrite an existing file.
noexec Same as -n
noglob Same as -f
nolog Function definitions are not stored in the history file.
nounset Same as -u
privileged Same as -p
protected Same as -p, except PATH is reset.
verbose Same as -v
vi The vi style in-line editor is used for command entry. See In-line Command Editing.
viraw Specifies vi character-by-character input.
xtrace Same as -x
-p Toggles the privileged mode on and off. If on, the effective user and group IDs are set to the real ones. If off, the effective IDs are restored to their values when the shell was initially invoked. Thus privileged is on when the effective ID does not match a real ID. When privileged mode is on, the shell
* Disables processing of the $HOME/.profile file.
* Uses the /etc/suid_profile instead of your ENV file.
-s Sorts the positional parameters.
-t Read and execute one command then exit the shell.
-u Unset variables cause an error when substitution is performed. If a variable has not been assigned a value and it is referenced, then an error will occur.
-v Verbose; echo (display) each shell input line as read.
-x Debug; display command lines as they are executed, each command line is preceded by a +.
-A Assign arguments arg to array name in sequential order beginning with element 0.
+opt If any of the above options are preceded by a + (plus sign) instead of a - (hyphen), the option will be disabled or turned off.
If no options or arguments are given, set displays the local environment.

NOTE:

The set command can be used to change or set options and arguments once a shell has begun execution. Set cannot change or set the -c, -i, -r, and -s options from within the current shell.


The following list describes the options and their arguments that may be used to control how unset functions.

-f The shell unsets only functions that you list on the command line. Variables are not affected.

Arguments

The following list describes the arguments that may be passed to the set commands.

args Valid arguments are programs or shell scripts that are executable and their arguments. These args are referred to as positional parameters (see Module 99, Parameters/Variables/Arrays). Parameters may be keyword parameters, constants, tilde substitution, command substitution, or filename generation.

Csh Shell
(wordlist) A list of words that are assigned to the named variable. Command and filename expansion is performed. Variable expansion is also performed before the list of words are assigned to the variable. Multiple lists may be specified.
$< Reads one line from standard input.

The following list describes the arguments that may be passed to the unset command.

variables Shell variables that have been created. They may or may not be set to a value, but they do exist in the environment.
functions Shell functions that have been defined (ksh only).

FURTHER DISCUSSION

The set command provides three functions. You can display the names and values of all variables (parameters) by typing set with no options or arguments. You can set positional parameters by typing

     cj> set positional_parameter1 positional_parameter2

and pressing Return. You may also want to use set to change the current options of your shell or shell script. For example, you may be writing a script and have a syntax error. To debug the script you can insert a line containing set -x to have each line displayed as it is executed. This is equivalent to typing sh -x shell_script.

The following examples help clarify some of these options.

    cj> ksh -x myscript

    cj> START=10 ksh -k myscript INCREMENT=2

The -x can be used to debug shell scripts. The -k option allows you to place keyword assignments after the command has been entered.

To display what options are currently set for the shell type

     echo $-

and press Return.

The unset command will remove a variable or a function from your current shell environment. It will not remove a readonly variable. It cannot remove the PATH, PS1, PS2, MAILCHECK, or IFS variables.

RELATED COMMANDS

Refer to the ksh command described in Module 71 and the shift command in Module 120.

APPLICATIONS

The set command may be used to set or reset options for the current shell, thus allowing you to change how the shell performs in-line, without having to restart your shell (log out and log in again).

You can also use set to set and reset positional parameters. This provides a simple way to work with columns of data separated by the character(s) defined in the IFS variable. You can cat a file to a loop structure, read one line per iteration, and use set to assign the line to positional parameters. Each word of input becomes a parameter.

The unset command is used to completely remove a variable or function from the shell environment. You may decide you no longer want a variable to exist in the current environment. If you are going to invoke a command or subshell, you may not want to pass your full environment to the new child process. By unsetting the variables you prevent the new process from knowing about the variable.

TYPICAL OPERATION

In this activity you use the set command to process a line of input from a file inside a shell while loop. Begin at the shell prompt.

If you are using the csh, type sh and press Return to enter a Bourne shell for the following examples. Eliminate the "--" and escape sequences from the echo commands if present. Change the "\t" to tabs.

1.  Type the following lines, pressing Return after each line.
   cj> OIFS=${IFS}
   IFS=":"
   cat /db/phone | \
   while read LINE
    do
     set -- ${LINE}
     echo "$1\t$4\t$5"
    done
   IFS=${OIFS}

This code will list out each line of the /db/phone file and format it into a readable table. The IFS=: changes your field (argument) separator to a colon. The while loop reads one line of input per iteration of the loop. When read can no longer read input (EOF is read) then loop terminates. The set -- ${LINE} sets the input line to be positional parameters. The backslash t (\t) tells echo to display a tab. You must reset your IFS variable back to normal so you can pass arguments correctly.

C Shell

1.  There is no easy or reliable way to duplicate this code in the csh. But you can reset the command line arguments using the set command. Simply type the following.
cj> set argv=(newarg1 newarg2 newargN)
2.  Now type echo $argv[2] and press Return.
2.  The set command can list out your local environment. Type set and press Return to list the environment. In contrast to set displaying your local environment, you can use env or printenv (BSD) to display your global environment.

In this activity you use the unset command to remove a variable from your current environment. Begin at the shell prompt.

1.  Type MYVAR="Here is a new variable defined" and press Return.
2.  Display the value of MYVAR by typing echo $MYVAR and pressing Return. Your screen will look like this:
   cj> echo $MYVAR
   Here is a new variable defined
3.  Type set and press Return to display all variables in your shell environment. Notice the MYVAR variable and its value.
4.  Now remove the MYVAR variable by typing unset MYVAR and pressing Return.
5.  Type set and press Return again to display your environment. Notice that the MYVAR variable is not listed. It no longer exists in your environment.
6.  Turn to Module 119 to continue the learning sequence.

C Shell
The set command is used in the csh to set variables. The following activity illustrates the use of the set command.
1. Type set term=unkown and press Return.
2. Now type set and press Return to display all set variables.
3. Type set term=vt100 and press Return. Replace vt100 with the type of terminal you are using.
4. Turn to Module 119 to continue the learning sequence.


Previous Table of Contents Next