Classic Shell Scripting - Arnold Robbins [68]
Caveats
When printing, env does not necessarily quote environment variable values correctly for re-inputting to the shell. Use export -p for that.
* * *
The unset command removes variables and functions from the running shell. By default it unsets variables, although this can be made explicit with -v:
unset full_name Remove the full_name variable
unset -v first middle last Remove the other variables
Use unset -f to remove functions:
who_is_on ( ) { Define a function
who | awk '{ print $1 }' | sort -u Generate sorted list of users
}
...
unset -f who_is_on Remove the function
Early versions of the shell didn't have functions or the unset command. POSIX added the -f option for removing functions, and then added the -v option for symmetry with -f.
* * *
unset
Usage
unset [ -v ] variable ...
unset -f function ...
Purpose
To remove variables and functions from the current shell.
Major options
-f
Unset (remove) the named functions.
-v
Unset (remove) the named variables. This is the default action with no options.
Behavior
With no options, arguments are treated as variable names and said variables are removed. The same occurs with the -v option. With the -f option, arguments are treated as function names and the functions are removed.
* * *
* * *
Tip
The assignment myvar= doesn't remove myvar, it merely sets it to the null string. In contrast, unset myvar removes it completely. This difference comes into play with the various "is the variable set" and "is the variable set but not null" expansions described in the next section.
* * *
Parameter Expansion
Parameter expansion is the process by which the shell provides the value of a variable for use in the program; e.g., as the value for a new variable, or as part or all of a command-line argument. The simplest form is undoubtedly familiar:
reminder="Time to go to the dentist!" Save value in reminder
sleep 120 Wait two hours
echo $reminder Print message
The shell has more complicated forms that are useful in more specialized situations. All of these forms enclose the variable's name in braces (${variable}), and then add additional syntax telling the shell what to do. Braces by themselves are also useful, should you need to immediately follow a variable name with a character that might otherwise be interpreted as part of the name:
reminder="Time to go to the dentist!" Save value in reminder
sleep 120 Wait two hours
echo _${reminder}_ Print message with underscores, for emphasis
* * *
Warning
By default, undefined variables expand to the null (empty) string. Sloppy programming can thus lead to disaster:
rm -fr /$MYPROGRAM If MYPROGRAM isn't set, disaster strikes!
It thus pays, as always, to program carefully!
* * *
Expansion operators
The first group of string-handling operators tests for the existence of variables and allows substitutions of default values under certain conditions. They are listed in Table 6-1.
Table 6-1. Substitution operators
Operator
Substitution
${ varname :- word }
If varname exists and isn't null, return its value; otherwise, return word.
Purpose: To return a default value if the variable is undefined.
Example: ${count:-0} evaluates to 0 if count is undefined.
${ varname := word }
If varname exists and isn't null, return its value; otherwise, set it to word and then return its value.
Purpose: To set a variable to a default value if it is undefined.
Example: ${count:=0} sets count to 0 if it is undefined.
${ varname :? message }
If varname exists and isn't null, return its value; otherwise, print varname: message, and abort the current command or script. Omitting message produces the default message parameter null or not set. Note, however, that interactive shells do not have to abort. (Behavior varies across shells; caveat emptor!)
Purpose: To catch errors that result from variables being undefined.
Example: ${count:?"undefined!"} prints count: undefined!