While aliases let you create convenient names for commands, they don't really let you change the shell's behavior. Options are one way of doing this. A shell option is a setting that is either "on" or "off." While several options relate to arcane shell features that are of interest only to programmers, those that we will cover here are of interest to all users.
The basic commands that relate to options are set -o optionname and set + o optionname . You can change more than one option with the one set command by preceding each optionname with a -o or +o . The use of plus ( + ) and minus ( - ) signs is counterintuitive: the - turns the named option on, while the + turns it off. The reason for this incongruity is that the dash ( - ) is the conventional UNIX way of specifying options to a command, while the use of + is an afterthought.
Most options also have one-letter abbreviations that can be used in lieu of the set -o command; for example, set -o noglob can be abbreviated set -f . These abbreviations are carryovers from the Bourne shell. Like several other "extra" bash features, they exist to ensure upward compatibility; otherwise , their use is not encouraged.
Table 3.1 lists the options that are useful to general UNIX users. All of them are off by default except as noted.
Table 3.1. Basic Shell Options | |
Option | Description |
emacs | Enter emacs editing mode (on by default) |
ignoreeof | Don't allow use of a single CTRL-D to log off; use the exit command to log off immediately. This has the same effect as setting the shell variable IGNOREEOF=10. |
noclobber | Don't allow output redirection ( > ) to overwrite an existing file |
noglob | Don't expand filename wildcards like * and ? (wildcard expansion is sometimes called globbing ) |
nounset | Indicate an error when trying to use a variable that is undefined |
vi | Enter vi editing mode |
There are several other options (21 in all; Appendix B , lists them). To check the status of an option, just type set -o . bash will print a list of all options along with their settings.
bash 2.0 introduces a new built-in for configuring shell behaviour, shopt . This built-in is meant as a replacement for option configuration originally done through environment variables and the set command. [6]
[6] Appendix B provides a complete list of shopt shell options and the corresponding environment variables in earlier versions of the shell.
The shopt -o functionality is a duplication of parts of the set command and is provided for completeness on the part of shopt , while retaining backward compatibility by its continued inclusion in set .
The format for this command is shopt options option-names . Table 3.2 lists shopt 's options.
Table 3.2. Options to shopt | |
Option | Meaning |
-p | Display a list of the settable options and their current values |
-s | Sets each option name |
-u | Unset each option name |
-q | Suppress normal output; the return status indicates if a variable is set or unset |
-o | Allows the values of the option names to be those defined for the -o option of the set command |
The default action is to unset ( turn off) the named options. If no options and arguments are given, or the -p option is used, shopt displays a list of the settable options and the values that they currently have. If -s or -u is also given, the list is confined to only those options that are set or unset, respectively.
A list of the most useful option names is given in Table 3.3 . A complete list is given in Appendix B .
Table 3.3. shopt Option Names | |
Option | Meaning |
cdable_vars | If set, an argument to the cd built-in command that is not a directory is assumed to be the name of a variable whose value is the directory to change to. |
checkhash | If set, bash checks that a command found in the hash table exists before trying to execute it. If a hashed command no longer exists, a normal path search is performed. |
cmdhist | If set, bash attempts to save all lines of a multiple-line command in the same history entry. |
dotglob | If set, bash includes filenames beginning with a . (dot) in the results of pathname expansion. |
execfail | If set, a non-interactive shell will not exit if it cannot execute the file specified as an argument to the exec command. An interactive shell does not exit if exec fails. |
histappend | If set, the history list is appended to the file named by the value of the HISTFILE variable when the shell exits, rather than overwriting the file. |
lithist | If set, and the cmdhist option is enabled, multiline commands are saved to the history with embedded newlines, rather than using semicolon separators where possible. |
mailwarn | If set, and a file that bash is checking for mail has been accessed since the last time it was checked, the message "The mail in mailfile has been read" is displayed. |
We'll look at the use of the various options later in this chapter.