Controlling bash Features and Options


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. lists some commonly used command line options.

Table 9-12. Command line options

Option

Explanation

Syntax

Help

Displays a usage message.

--help

No edit

Prevents users from using the Readline Library (page ) to edit command lines in an interactive shell.

--noediting

No profile

Prevents reading these startup files (page ): /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile.

--noprofile

No rc

Prevents reading the ~/.bashrc startup file (page ). This option is on by default if the shell is called as sh.

--norc

POSIX

Runs bash in POSIX mode.

--posix

Version

Displays bash version information and exits.

--version

Login

Causes bash to run as though it were a login shell.

-l (lowercase "l")

Shopt

Runs a shell with the opt shopt option (page ). A -O (uppercase "O") sets the option; +O unsets it.

[±]O [opt]

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 (-).

--


Shell Features

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, 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 ):

$ 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. lists bash features.

Table 9-13. bash feature

Feature

Description

Syntax

Alternate syntax

allexport

Automatically exports all variables and functions that you create or modify after giving this command.

set -o allexport

set -a

braceexpand

Causes bash to perform brace expansion (the default; page ).

set -o braceexpand

set -B

cdspell

Corrects minor spelling errors in directory names used as arguments to cd.

shopt -s cdspell

 

cmdhist

Saves all lines of a multiline command in the same history entry, adding semicolons as needed.

shopt -s cmdhist

 

dotglob

Causes shell special characters (wildcards; page ) 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

 

emacs

Specifies emacs editing mode for command line editing (the default; page ).

set -o emacs

 

errexit

Causes bash to exit when a simple command (not a control structure) fails.

set -o errexit

set -e

execfail

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

 

expand_aliases

Causes aliases (page ) to be expanded (by default it is on for interactive shells and off for noninteractive shells).

shopt -s expand_alias

 

hashall

Causes bash to remember where commands it has found using PATH (page ) are located (default).

set -o hashall

set -h

histappend

Causes bash to append the history list to the file named by HISTFILE (page ) when the shell exits. By default bash overwrites this file.

shopt -s histappend

 

histexpand

Causes the history mechanism (which uses exclamation points; page ) to work (default). Turn this feature off to turn off history expansion.

set -o histexpand

set -H

history

Enable command history (on by default; page ).

set -o history

 

ignoreeof

Specifies that bash must receive ten EOF characters before it exits. Useful on noisy dial-up lines.

set -o ignoreeof

 

monitor

Enables job control (on by default, page ).

set -o monitor

set -m

nocaseglob

Causes ambiguous file references (page ) to match filenames without regard to case (off by default).

shopt -s nocaseglob

 

noclobber

Helps prevent overwriting files (off by default; page ).

set -o noclobber

set -C

noglob

Disables pathname expansion (off by default; page ).

set -o noglob

set -f

notify

With job control (page ) 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

set -b

nounset

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

set -u

nullglob

Causes bash to expand ambiguous file references (page ) that do not match a filename to a null string. By default bash passes these file references without expanding them.

shopt -s nullglob

 

posix

Runs bash in POSIX mode.

set -o posix

 

verbose

Displays command lines as bash reads them.

set -o verbose

set -v

vi

Specifies vi editing mode for command line editing (page ).

set -o vi

 

xpg_echo

Causes the echo builtin to expand backslash escape sequences without the need for the -e option (page ).

shopt -s xpg_echo

 

xtrace

Turns on shell debugging (page ).

set -o xtrace

set -x


shopt: Turns Shell Features On and Off

The shopt (shell option) builtin 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. lists bash features.

Tip: Setting set ±o 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