Bash shopt builtin command

Updated: 05/04/2019 by Computer Hope
shopt command

On Unix-like operating systems, shopt is a builtin command of the Bash shell which can enable or disable options for the current shell session.


Use shopt to change any of the Options listed below.

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


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.


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

Option name Description
autocdIf set, a command name that is the name of a directory is executed as if it were the argument to the cd command. This option is only used by interactive shells. For more information about interactive shells, see invoking the bash shell.
cdable_varsIf set, an argument to the cd builtin command that is not a directory is assumed to be the name of a variable whose value is the directory to change to.
cdspellIf set, minor errors in the spelling of a directory component in a cd command will be 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.
checkhashIf 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.
checkjobsIf 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.
checkwinsizeIf 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.
cmdhistIf 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.
compat31If set, bash changes its behavior to that of bash version 3.1 with respect to quoted arguments to the [[ conditional command's =~ operator.
compat32If set, bash changes its behavior to that of bash version 3.2 with respect to locale-specific string comparison when using the [[ 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.
compat40If set, bash changes its behavior to that of bash version 4.0 with respect to locale-specific string comparison when using the [[ conditional command's < and > operators (see previous item) and the effect of interrupting a command list.
compat41If 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.
direxpandIf 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.
dirspellIf set, bash attempts spelling correction on directory names during word completion if the directory name initially supplied does not exist.
dotglobIf set, bash includes file names beginning with a '.' in the results of path name globbing.
execfailIf set, a non-interactive shell will 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_aliasesIf set, aliases are expanded as described in bash aliases. This option is enabled by default for interactive shells.
extdebugIf 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 command run by the DEBUG trap returns a value of 2, and the shell is executing in a subroutine (a shell function or a shell script executed by the . or source builtins), 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.
extglobIf set, the extended pattern matching features offered by bash path name expansion are enabled.
extquoteIf set, $'string' and $"string" quoting is performed within ${parameter} expansions enclosed in double quotes. This option is enabled by default.
failglobIf set, patterns which fail to match file names during path name expansion result in an expansion error.
force_fignoreIf 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.
globstarIf set, the pattern ** used in a path name expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.
gnu_errfmtIf set, shell error messages are written in the standard GNU error message format.
histappendIf set, the history list is appended to the file named by the value of the HISTFILE variable when the shell exits, rather than overwriting the file.
histreeditIf set, and readline is being used, a user is given the opportunity to re-edit a failed history substitution. For more information about history substitution, see bash event designators.
histverifyIf set, and readline is being 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.
hostcompleteIf set, and readline is being used, bash will attempt to perform hostname completion when a word containing a @ is being completed (see bash readline completion for more information). This is enabled by default.
huponexitIf set, bash will send the SIGHUP signal to all jobs when an interactive login shell exits.
interactive_commentsIf set, allow a word beginning with # to cause that word and all remaining characters on that line to be ignored in an interactive shell (see bash comments for more information). This option is enabled by default.
lastpipeIf 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.
lithistIf 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.
mailwarnIf set, and a file that bash is checking for mail was accessed since the last time it was checked, the message "The mail in mailfile has been read" is displayed.
no_empty_cmd_completionIf set, and readline is being used, bash will not attempt to search the PATH for possible completions when completion is attempted on an empty line.
nocaseglobIf set, bash matches file names in a case-insensitive fashion when performing path name expansion.
nocasematchIf set, bash matches patterns in a case-insensitive fashion when performing matching while executing case or [[ conditional commands.
nullglobIf set, bash allows patterns which match no files to expand to a null string, rather than themselves.
progcompIf set, the programmable completion facilities are enabled. For more information, see programmable completion in bash. This option is enabled by default.
promptvarsIf 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_verboseIf set, the shift builtin prints an error message when the shift count exceeds the number of positional parameters.
sourcepathIf 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.



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:

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:


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