Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
This chapter is from the book

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

Option

Explanation

Syntax

Help

Displays a usage message.

--help

No edit

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

--noediting

No profile

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

--noprofile

No rc

Prevents reading the ~/.bashrc startup file (page 260). 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.

--I (lowercase "I")

shopt

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

[±]0 [ 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.

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

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

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 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

 

emacs

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

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 311) 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 285) are located (default).

set –o hashall

set –h

histappend

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

 

histexpand

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

set –H

history

Enable command history (on by default; page 295).

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

set –o monitor

set –m

nocaseglob

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

shopt –s nocaseglob

 

noclobber

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

set –o noclobber

set –C

noglob

Disables pathname expansion (off by default; page 133).

set –o noglob

set –f

notify

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

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 133) 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 304).

set –o vi

 

xpg_echo

Causes the echo builtin to expand back-slash escape sequences without the need for the –e option (page 548).

shopt –s xpg_echo

xtrace

Turns on shell debugging (page 536).

set –o xtrace

set –x

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.

  • + Share This
  • 🔖 Save To Your Account