Open In App

Shell Scripting – Shopt Command

Last Updated : 17 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Unlike many shell commands, shopt is a bash-only shell command. It is a built-in BASH shell command that controls certain options for the shell session. One can consider this as a boolean ON/OFF switch for certain shell options. The “shopt” command provides control over many settings that are used to tweak the operations in a Bash shell. The number of options are available for the “shopt” command varies from version to version; older versions will have fewer commands compared to newer versions.

Syntax:

shopt [arg] [optname...]

The different values of arg and optname will be discussed below sections.

Values of the argument arg

arg description
-o Restricts the optname values to those of the built-in set command
-p Prints the current values of all shell options.
-q

This is the quiet mode; provides no output but, returns an error status.

For the single optname provided, this return status TRUE(0) if the option is set, else it returns FALSE(non-zero).

For multiple optname(s) provided, this return error status TRUE(0) if all the given options are set, else return FALSE(non-zero).

-s Sets the given optnames. If none is given then, displays all the set options.
-u Simply unsets the options given.

Values of the optname options

The following table displays the options given to the shopt command’s optnames.

optname description 
autocd If 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.
cdable_vars If set, an argument to the cd built-in command that is not a directory is assumed to be the name of a variable whose value is the directory to change to.
cdspell If 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 filename is printed, and the command proceeds.   Interactive shells only use this option.
checkhash If set, bash checks that a command found in the hash table exists before trying to execute it. If a hashed command no longer exists, a normal path search is performed.
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.
checkwinsize If set, bash checks the window size after each external (non-builtin) command and, if necessary, updates the values of LINES and COLUMNS. This option is enabled by default.
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 multiline commands. This option is enabled by default but only has an effect if command history is enabled.
compat31 These control aspects of the shell’s compatibility mode.
compat40
compat41
direxpand

If set, bash replaces directory names with the results of word expansion when performing filename completion. 

This changes the contents of readline editing buffer.

If not set, bash tries to preserve the user typed input.

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 filenames beginning with a ‘.’ in the results of pathname expansion.

The filenames ”.” and ”..” must be matched explicitly, even if dotglob is set. 

execfail If set, a non-interactive shell will not exit if it cannot execute the file specified as an argument to the built-in exec command.
expand_aliases If set, aliases are expanded. This option is set by default for interactive shells.
extglob If set, the extended pattern matching features are enabled.
extquote

If set, $’string’ and $”string” quoting is performed within ${parameter} expansions enclosed in double quotes. 

This is enabled by default.

failglob If set, patterns that fail to match filenames during pathname 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.

This is set by default.

globstar

If set, the pattern ** used in a pathname expansion context will match all files and zero or more directories and sub-directories.

If the pattern is followed by a /, only directories and sub-directories 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 value of the HISTFILE variable when the shell exists, rather than overwriting the file.
histreedit If set, and readline is being used, a user is given the opportunity to re-edit a failed history substitution.
histverify If set and readline are 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.
hostcomplete

If set, and readline is being used, bash will attempt to perform hostname completion when a word containing a @ is being completed.

This is set by default.

huponexit

If set, command substitution inherits the value of the exit option, instead of unsetting it in the subshell environment. 

This option is enabled when POSIX mode is enabled.

interactive_comments

If set, allows a word beginning with # to cause that word and all remaining characters on that line to be ignored(commented)  in an interactive shell.

This is set by default.

lastpipe If set, and the job 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 has been accessed since the last time it was checked, the message ”The mail in mailfile has been read” is displayed.
no_empty_cmd_completion If 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.
nocaseglob If set, bash matched filenames in a case-insensitive fashion when performing pathname expansion.
nocasematch If set, bash matches patterns in a case-insensitive fashion when performing matching while executing case or [[ conditional commands, when performing pattern substitution word expansion, or when filtering possible completions as part of programmable completion.
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. 

This option is enabled by default.

promptvars

If set, prompt strings undergo parameter expansion, command substitution, arithmetic expansion, and quote removal after being expanded.

This is set by default.

shift_verbose If set, the shift built-in prints an error message when the shift count exceeds the number of positional parameters.
sourcepath

If set, the source (.) built-in uses the value of PATH to find the directory containing the file supplied as an argument. 

This is set by default.

xpg_echo If set, the echo built-in expands backslash-escape sequences by default.

Now, providing a description of all these options is a clumsy task therefore, in the next section it is shown how can one find the proper description of these options in the 6000+ lines long bash manual.

Accessing bash manual

The bash manual is highly descriptive and easy to access if one knows how. To open the bash manual, type the following command:

man bash
Bash Manual in linux terminal

 

Now, it is a highly technical manual and scrolling to search for one keyword is hectic luckily, a simpler way exists. Once the manual is open, type / followed by the keyword you need a description about then, press enter.

/<optname>
#press enter 

Type the required keyword as shown after pressing /

 

The manual where that keyword is located, comes to the terminal screen.

 

Examples of shopt command

Example 1: To print all options and their values.

shopt -p

 

Example 2: To print the set optnames and their values.

shopt -s

 

Example 3: Setting the xpg_echo option

shopt -s xpg_echo
Escape sequences are enbled.

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads