Controlling bash Features and Options
This section explains how to control bash features and options using command line options and the set and shopt builtins.
Command Line Options
Two kinds of command line options are available: short and long. Short options consist of a hyphen followed by a letter; long options have two hyphens followed by multiple characters. Long options must appear before short options on a command line that calls bash. Table 8-12 lists some commonly used command line options.
Table 8-12. Command line options
Displays a usage message.
Prevents users from using the Readline Library (page 304) to edit command lines in an interactive shell.
Prevents reading these startup files (page 259): /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile.
Prevents reading the ~/.bashrc startup file (page 260). This option is on by default if the shell is called as sh.
Runs bash in POSIX mode.
Displays bash version information and exits.
Causes bash to run as though it were a login shell.
--I (lowercase "I")
Runs a shell with the opt shopt option (page 318). A -0 (uppercase "0") sets the option; +0 unsets it.
End of options
On the command line, signals the end of options. Subsequent tokens are treated as arguments even if they begin with a hyphen (-).
You can control the behavior of the Bourne Again Shell by turning features on and off. Different features use different methods to turn features on and off. The set builtin controls one group of features, while the shopt builtin controls another group. You can also control many features from the command line you use to call bash.
Tip: Features, options, variables?
To avoid confusing terminology, this book refers to the various shell behaviors that you can control as features. The bash info page refers to them as "options" and "values of variables controlling optional shell behavior."
set ±o: Turns Shell Features On and Off
The set builtin (there is a set builtin in tcsh, but it works differently), when used with the -o or +o option, enables, disables, and lists certain bash features. For example, the following command turns on the noclobber feature (page 125):
$ set -o noclobber
You can turn this feature off (the default) by giving the command
$ set +o noclobber
The command set -o without an option lists each of the features controlled by set followed by its state (on or off). The command set +o without an option lists the same features in a form that you can use as input to the shell. Table 8-13 lists bash features.
Table 8-13. bash features
Automatically exports all variables and functions that you create or modify after giving this command.
set o allexport
Causes bash to perform brace expansion (the default; page 323).
set o braceexpand
Corrects minor spelling errors in directory names used as arguments to cd.
shopt s cdspell
Saves all lines of a multiline command in the same history entry, adding semicolons as needed.
shopt s cmdhist
Causes shell special characters (wildcards; page 133) in an ambiguous file reference to match a leading period in a filename. By default special characters do not to match a leading period. You must always specify the filenames . and .. explicitly because no pattern ever matches them.
shopt s dotglob
Specifies emacs editing mode for command line editing (the default; page 305).
set o emacs
Causes bash to exit when a simple command (not a control structure) fails.
set o errexit
Causes a shell script to continue running when it cannot find the file that is given as an argument to exec. By default a script terminates when exec cannot find the file that is given as its argument.
shopt s execfail
Causes aliases (page 311) to be expanded (by default it is on for interactive shells and off for noninteractive shells).
shopt s expand_alias
Causes bash to remember where commands it has found using PATH (page 285) are located (default).
set o hashall
Causes bash to append the history list to the file named by HISTFILE (page 295) when the shell exits. By default bash overwrites this file.
shopt s histappend
Causes the history mechanism (which uses exclamation points; page 299) to work (default). Turn this feature off to turn off history expansion.
set o histexpand
Enable command history (on by default; page 295).
set o history
Specifies that bash must receive ten EOF characters before it exits. Useful on noisy dial-up lines.
set o ignoreeof
Enables job control (on by default, page 272).
set o monitor
Causes ambiguous file references (page 133) to match filenames without regard to case (off by default).
shopt s nocaseglob
Helps prevent overwriting files (off by default; page 125).
set o noclobber
Disables pathname expansion (off by default; page 133).
set o noglob
With job control (page 272) enabled, reports the termination status of background jobs immediately. The default behavior is to display the status just before the next prompt.
set o notify
Displays an error and exits from a shell script when you use an unset variable in an interactive shell. The default is to display a null value for an unset variable.
set o nounset
Causes bash to expand ambiguous file references (page 133) that do not match a filename to a null string. By default bash passes these file references without expanding them.
shopt s nullglob
Runs bash in POSIX mode.
set o posix
Displays command lines as bash reads them.
set o verbose
Specifies vi editing mode for command line editing (page 304).
set o vi
Causes the echo builtin to expand back-slash escape sequences without the need for the e option (page 548).
shopt s xpg_echo
Turns on shell debugging (page 536).
set o xtrace
shopt: Turns Shell Features On and Off
The shopt (shell option) builtin (not available in tcsh) enables, disables, and lists certain bash features that control the behavior of the shell. For example, the following command causes bash to include filenames that begin with a period (.) when it expands ambiguous file references (the -s stands for set):
$ shopt -s dotglob
You can turn this feature off (the default) by giving the command (the u stands for unset)
$ shopt -u dotglob
The shell displays how a feature is set if you give the name of the feature as the only argument to shopt:
$ shopt dotglob dotglob off
The command shopt without any options or arguments lists the features controlled by shopt and their state. The command shopt -s without an argument lists the features controlled by shopt that are set or on. The command shopt u lists the features that are unset or off. Table 8-13 lists bash features.
Tip: Setting set ±0 features using shopt
You can use shopt to set/unset features that are otherwise controlled by set ±o. Use the regular shopt syntax with s or u and include the o option. For example, the following command turns on the noclobber feature:
$ shopt -o -s noclobber