Bash shopt builtin command

Updated: 12/31/2020 by Computer Hope
shopt command

On Unix-like operating systems, shopt is a builtin command of the Bash shell that enables or disables options for the current shell session.

Description

Use shopt to change any of the Options listed below.

shopt is similar to, but different than, the set builtin. To change the behavior of bash as if it had been invoked with different command line options, use set.

Syntax

shopt [-o] [-p] [-q] [-s] [-u] [optname...]

The shopt builtin command takes the following options and arguments:

optname The name of a bash option. Multiple option names can be given, separated by a space. For a list and description of all bash options, see Options, below.
-o Restrict the values of optname to those known by the set builtin command.
-p Print all shell options, and their current values. This is the default behavior if no options are given.
-q Quiet mode. Provide no output, but return an error status.

For a single optname, the error status is TRUE (0) if the option is set, otherwise the error status is FALSE (non-zero).

For multiple optnames, the error status is TRUE if all options are set, and FALSE if any of the options are not set.
-s If optnames are specified, set those options. If no optnames are specified, list all options that are currently set.
-u Unset optnames.

Options

The following options modify the behavior of bash, and can be set or unset using shopt.

Option name Description
autocd If set, a command name that is a directory name is executed as if it were the cd command's argument. This option is only used by interactive shells. For more information about interactive shells, see invoking the bash shell.
cdable_vars If set, a cd command's argument that is not a directory is assumed to be the variable's name whose value is the directory to change to.
cdspell If set, minor errors in the spelling of a directory component in a cd command are corrected. The errors checked for are transposed characters, a missing character, and one character too many. If a correction is found, the corrected file name is printed, and the command proceeds. This option is only used by interactive shells.
checkhash If set, bash checks that a command found in the hash table of remembered commands actually exists before trying to execute it. If a hashed command no longer exists, a normal path search is performed. For more information about the hash table maintained by bash, see the hash builtin command.
checkjobs If set, bash lists the status of any stopped and running jobs before exiting an interactive shell. If any jobs are running, this causes the exit to be deferred until a second exit is attempted without an intervening command. The shell always postpones exiting if any jobs are stopped. For more information about jobs, see bash job control.
checkwinsize If set, bash checks the window size of the current terminal window after each command, and, if necessary, updates the values of the LINES and COLUMNS shell variables.
cmdhist If set, bash attempts to save all lines of a multiple-line command in the same history entry. This allows easy re-editing of multi-line commands. For more information about how the bash command history works, see history in bash.
compat31 If set, bash changes its behavior to that of bash version 3.1 with respect to quoted arguments to [[ conditional command's =~ operator.
compat32 If set, bash changes its behavior to that of bash version 3.2 with respect to locale-specific string comparison when using [[ conditional command's < and > operators. Bash versions before bash-4.1 use ASCII collation and strcmp; bash-4.1 and later use the current locale's collation sequence and strcoll.
compat40 If set, bash changes its behavior to that of bash version 4.0 with respect to locale-specific string comparison when using [[ conditional command's < and > operators (see previous item) and the effect of interrupting a command list.
compat41 If set, bash, when in POSIX mode, treats a single quote in a double-quoted parameter expansion as a special character. The single quotes must match (an even number) and the characters between the single quotes are considered quoted. This is the behavior of POSIX mode through bash version 4.1. The default bash behavior remains as in previous versions. For more information about using bash in POSIX mode, see invoking bash.
direxpand If set, bash replaces directory names with the results of word expansion when performing file name completion. This changes the contents of the readline editing buffer. If not set, bash attempts to preserve what the user typed.
dirspell If set, bash attempts spelling correction on directory names during word completion if the directory name initially supplied does not exist.
dotglob If set, bash includes file names beginning with a '.' in the results of path name globbing.
execfail If set, a non-interactive shell does not exit if it cannot execute the file specified as an argument to the exec builtin command. An interactive shell does not exit if exec fails.
expand_aliases If set, aliases are expanded as described in bash aliases. This option is enabled by default for interactive shells.
extdebug If set, behavior intended for use by debuggers is enabled:

  1. The -F option to the declare builtin displays the source file name and line number corresponding to each function name supplied as an argument.
  2. If the command run by the DEBUG trap returns a non-zero value, the next command is skipped and not executed.
  3. If the DEBUG trap's command returns a value of 2, and the shell is executing in a subroutine, a call to return is simulated.
  4. BASH_ARGC and BASH_ARGV are updated as described in their descriptions.
  5. Function tracing is enabled: command substitution, shell functions, and subshells invoked with ( command ) inherit the DEBUG and RETURN traps.
  6. Error tracing is enabled: command substitution, shell functions, and subshells invoked with ( command ) inherit the ERR trap.
extglob If set, the extended pattern matching features offered by bash path name expansion are enabled.
extquote If set, $'string' and $"string" quoting is performed within ${parameter} expansions enclosed in double quotes. This option is enabled by default.
failglob If set, patterns which fail to match file names during path name expansion result in an expansion error.
force_fignore If set, the suffixes specified by the FIGNORE shell variable cause words to be ignored when performing word completion even if the ignored words are the only possible completions. See bash shell variables for a description of FIGNORE. This option is enabled by default.
globstar If set, the pattern ** used in a path name expansion context matches all files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.
gnu_errfmt If set, shell error messages are written in the standard GNU error message format.
histappend If set, the history list is appended to the file named by the HISTFILE variable's value when the shell exits, rather than overwriting the file.
histreedit If set, and readline is used, a user is given the opportunity to re-edit a failed history substitution. For more information about history substitution, see bash event designators.
histverify If set, and readline is used, the results of history substitution are not immediately passed to the shell parser. Instead, the resulting line is loaded into the readline editing buffer, allowing further modification.
hostcomplete If set, and readline is used, bash attempts to perform hostname completion when a word containing a @ is completed. This is enabled by default. (see bash readline completion for more information.)
huponexit If set, bash sends the SIGHUP signal to all jobs when an interactive login shell exits.
interactive_comments If set, allow a word beginning with # to cause that word and all remaining characters on that line to be ignored in an interactive shell. This option is enabled by default. (See bash comments for more information.)
lastpipe If set, and job control is not active, the shell runs the last command of a pipeline not executed in the background in the current shell environment.
lithist If set, and the cmdhist option is enabled, multi-line 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 was accessed after last checked, the message "The mail in mailfile has been read" is displayed.
no_empty_cmd_completion If set, and readline is used, bash does not attempt to search the PATH for possible completions when completion is attempted on an empty line.
nocaseglob If set, bash matches file names in a case-insensitive fashion when performing path name expansion.
nocasematch If set, bash matches patterns in a case-insensitive fashion when performing matching while executing case or [[ conditional commands.
nullglob If set, bash allows patterns which match no files to expand to a null string, rather than themselves.
progcomp If set, the programmable completion facilities are enabled. For more information, see programmable completion in bash. This option is enabled by default.
promptvars If set, prompt strings undergo parameter expansion, command substitution, arithmetic expansion, and quote removal after being expanded. For more information, see bash prompting. This option is enabled by default.
shift_verbose If set, the shift builtin prints an error message when the shift count exceeds the number of positional parameters.
sourcepath If set, the source builtin uses the value of the shell variable PATH to find the directory containing the file supplied as an argument. This option is enabled by default.

Examples

shopt

Print a list of all available options and their values.

shopt -p

Same as above; print a list of all available options and their values.

shopt -s

Print a list of all options that are currently set.

shopt -s globstar

Set the globstar option.

shopt -u cmdhist histappend

Unset the cmdhist and histappend options.

shopt -q globstar; echo $?

Check if the globstar option is set, and return an exit status instead of printing the result. In this case, the globstar option is unset, so the exit status is TRUE (zero). This value is stored in the ? shell variable, which is then printed to the terminal using echo. Output:

0
shopt -q globstar cmdhist histappend; echo $?

Return FALSE (a non-zero value) if any of globstar, cmdhist, or histappend options are not currently set. Output:

1

set — Set the value of shell options and positional parameters.