Bash Shell Options

A POSIX compliant shell can be configured with the set and unset builtins. To inspect the enabled options, use the $SHELLOPTS shell variable.

bash(1) expands on the set of configuration options. These options are set with the shopt builtins. To inspect the enabled options, use the $BASHOPTS shell variable.


Bash

Useful

If monitor is enabled, job control is used.

set -m #or: set -o monitor
set +m

If gnu_errfmt is enabled, error messages are written in the standard GNU format.

shopt -s gnu_errfmt
shopt -u gnu_errfmt

If lastpipe is enabled,the last command of a pipeline is executing in the current shell as opposed to a subshell.

shopt -s lastpipe
shopt -u lastpipe

Not Useful

To enter restricted shell mode, try:

set -r

The shell cannot leave restricted mode once entered.

To enter POSIX compatibility mode, try:

set -o posix

If privileged is enabled...

set -p #or: set -o privileged
set +p

If hashall is enabled, bash(1) stores executed commands in a hash table for later use. This is enabled by default.

If keyword is enabled, variable assignment is allowed anywhere in a command. As an example, make ARCH=ARM64 would be equivalent to ARCH=ARM64 make.

set -k #or: set -o keyword
set +k

If ignoreeof is enabled, an interactive shell will not exit upon receipt of an EOF character.

set -o ignoreeof

If sourcepath is enabled, the source builtin searches $PATH for the sourced file.

shopt -s sourcepath
shopt -u sourcepath

If checkhash is enabled, bash(1) checks for the existence of a command rather than assuming all commands in the command hash table exist.

shopt -s checkhash
shopt -u checkhash

login_shell and restricted_shell are set by bash(1) at startup, and cannot be changed. They simply allow inspection of the type of shell.


Changing Directories

If physical is enabled, symbolic links are not resolved by commands that change the working directory.

set -P #or: set -o physical
set +P

If autocd is enabled, when command expansion fails, the shell instead moves to the named directory.

shopt -s autocd
shopt -u autocd

If cdspell is enabled, simple spelling mistakes are automatically corrected. This includes transposed characters, one missing character, and one too many characters. To apply this functionality to all expansions, instead enable dirspell.

shopt -s cdspell
shopt -u cdspell

If cdable_vars is enabled, trying to move into a directory that can't be found causes the name to instead be expanded as a variable.

shopt -s cdable_vars
shopt -u cdable_vars


Comments

If interactive_comments is enabled, the comment character (#) works in interactive shells. This is enabled by default.


Compatibility Levels

bash(1) 4 introduced compatibility levels, wherein the behavior of the shell are modified.

shopt -s compat31
shopt -u compat31

The modes available through the shopt builtin are:

A shell only has one compatibility level, so each mode is mutually exclusive.

From bash(1) 4.3+, use of the $BASH_COMPAT shell variable is preferred.


Completion

If progcomp is enabled, programmable completion is used. This is enabled by default.

If progcomp_alias is enabled, when completion fails for a word, the word is checked for an alias. If an alias is registered, completion is re-attempted based on the aliased value.

If force_fignore is enabled and the $FIGNORE shell variable is set, the filtered filenames will be ignored by completion even if they are the only possible expansions. This is enabled by default.

If hostcomplete is enabled, hostname completion will be tried for any word containing @. This is enabled by default.

If direxpand is enabled, when filenames are expanded to an absolute path, the expansion is substituted into the command line.

shopt -s direxpand
shopt -u direxpand


Expansion

If braceexpand is enabled, brace expansion is applied. This is enabled by default.

If expand_aliases is enabled, aliases are expanded. By default, this is enabled for interactive shells.

If noglob is enabled, filename expansion is not applied.

set -f #or: set -o noglob
set +f

If globasciiranges is enabled, bracketed range expressions behave as though the C locale is set. In other words, [A-z] would be a portable way to express any letter.

shopt -s globasciiranges
shopt -u globasciiranges

If dotglob is enabled, filenames beginning with . are included in filename expansions.

shopt -s dotglob
shopt -u dotglob

If globstar is enabled, the pattern ** matches all files and subdirectories with zero or more directories in the path. Additionally, **/ will only match subdirectories with zero or more directories in the path.

shopt -s globstar
shopt -u globstar

If nocaseglob is enabled, filename expasion operates case-insensitively. To apply this functionality to the regular expression match operator as well, instead enable nocasematch.

shopt -s nocaseglob
shopt -u nocaseglob

If failglob is enabled, a filename pattern that does not expand to any filenames results in an error.

shopt -s failglob
shopt -u failglob

If nullglob is enabled, a filename pattern that does not expand to any filenames expands to a null string.

shopt -s nullglob
shopt -u nullglob

If dirspell is enabled, simple spelling mistakes are automatically corrected in filename expansion. This includes transposed characters, one missing character, and one too many characters. To apply this functionality only to the cd builtin, instead enable cdspell.

shopt -s dirspell
shopt -u dirspell

If no_empty_cmd_completion is enabled, when the command prompt is empty, no completion is attempted.

shopt -s no_empty_cmd_completion
shopt -u no_empty_cmd_completion

If extglob is enabled, extended pattern matching features become available.

shopt -s extglob
shopt -u extglob

If assoc_expand_once is enabled, associative array subscripts have expansion postponed.

shopt -s assoc_expand_once
shopt -u assoc_expand_once


History

If history is enabled, the history of commands is available for editting and re-execution. This behavior is configured with a set of shell variables. By default, this is enabled for interactive shells.

If cmdhist is enabled and command history is enabled, multi-line commands are collapsed to a single line when saving into history. This is enabled by default.

If lithist is enabled as well as cmdhist, multi-line commands are recorded in history with embedded newlines rather than semicolons.

shopt -s lithist
shopt -u lithist

If histappend is set, the history file is appended to rather than overwritten when the shell exits.

shopt -s histappend
shopt -u histappend

If histreedit is set and readline(3) is available, history can be re-editted.

shopt -s histreedit
shopt -u histreedit

If histexpand is enabled, history expansion is applied. By default, this is enabled for interactive shells.

If histverify is set, when a command is expanded with history expansion, the prompt reloads with the updated content (as opposed to the shell executing the updated command immediately).

shopt -s histverify
shopt -u histverify


Interactive Shells

If emacs is enabled, emacs-style cursor movement commands can be used on the command prompt.

set -o emacs

If vi is enabled, vi-style cursor movement commands can be used on the command prompt.

set -o vi

If notify is enabled, the status of terminated background jobs is printed immediately, as opposed to before printing the next prompt.

set -b #or: set -o notify
set +b

If promptvars is enabled, prompt strings undergo expansion after sequences are expanded. This is enabled by default.

If checkwinsize is enabled, bash(1) checks the window size after every (non-builtin) command. This triggers updates to the $LINES and $COLUMNS shell variables. By default, this is enabled for interactive shells.

If checkjobs is enabled, before an interactive shell is exitted, all stopped and running jobs will be listed. If there are any, the exit is postponed until another exit is attempted.

shopt -s checkjobs
shopt -u checkjobs

If huponexit is enabled, SIGHUP is sent to all jobs on exit.

shopt -s huponexit
shopt -u huponexit

Mail

If mailwarn is enabled, when a watched mail file has been accessed, a notification message is printed.

shopt -s mailwarn
shopt -u mailwarn


Programming

If verbose is enabled, commands are printed before executing.

set -v #or: set -o verbose
set +v

If allexport is enabled, all variables that are created or modified are set to export.

set -a #or: set -o allexport
set +a

If xpg_echo is enabled, the echo builtin expands ANSI sequences by default.

shopt -s xpg_echo
shopt -u xpg_echo

If nocasematch is enabled, the regular expression matching operator works case-insensitively. This also impacts expansion. To apply this functionality only to expansion, instead enable nocaseglob.

shopt -s nocasematch
shopt -u nocasematch

Errors

If errexit is enabled, a command returning an error code will cause the shell to exit in most circumstances. The exceptions are:

set -e #or: set -o errexit
set +e

If nounset is enabled, accessing an unset variable raises an error.

set -u #or: set -o nounset
set +u

If noclobber is enabled, output redirection with >, >&, and <> will raise an error rather than overwrite a file.

set -C #or: set -o noclobber
set +C

If pipefail is enabled, a pipeline returns the error code from the last command that exitted with a non-zero error code. A zero error code is only set if all commands exitted with a zero error code. This is enabled by default.

If shift_verbose is enabled, when the shift builtin is used exceeding the number of positional variables, an error message is emitted.

shopt -s shift_verbose
shopt -u shift_verbose

If execfail is enabled, when the exec builtin fails because the command could not be located, the non-interactive shell does not exit.

shopt -s execfail
shopt -u execfail

Subshell Inheritence

If inherit_errexit is enabled, the errexit setting propogates into subshells (chiefly in command substitutions).

shopt -s inherit_errexit
shopt -u inherit_errexit

If localvar_inherit is enabled, local variables are exported into subshells.

shopt -s localvar_inherit
shopt -u localvar_inherit

If localvar_unset is enabled, unsetting a local variable causes it to appear unset to subshells as well.

shopt -s localvar_unset
shopt -u localvar_unset

Traps

If errtrace is enabled, traps on ERR are inherited by subshells, shell functions, and command substitutions.

set -E #or: set -o errtrace
set +E

If functrace is enabled, traps on DEBUG and RETURN are inherited by subshells, shell functions, and command substitutions.

set -T #or: set -o errtrace
set +T


Quoting

If extquote is enabled, ANSI quoting and local translation quoting are performed within parameter expansion. This is enabled by default.

If complete_fullquote is enabled, filenames containing special characters are quoted during filename expansion. This is enabled by default.



Testing

If onecmd is enabled, the shell exits after executing one command.

set -t #or: set -o onecmd
set +t

If noexec is enabled, no commands will be executed.

set -n #or: set -o noexec
set +n


Tracing

If xtrace is enabled, simple commands as well as looping commands (and all arguments to these commands) are printed before execution.

set -x #or: set -o xtrace
set +x

If extdebug is enabled, external debuggers (like bashdb(1)) can be used.

shopt -s extdebug
shopt -u extdebug


CategoryRicottone

Bash/ShellOptions (last edited 2023-04-03 03:07:16 by DominicRicottone)