LinuxCommandLibrary

shopt

Set or display shell options

TLDR

List of all settable options and whether they are set

$ shopt
copy

Set an option
$ shopt -s [option_name]
copy

Unset an option
$ shopt -u [option_name]
copy

Print a list of all options and their status formatted as runnable shopt commands
$ shopt -p
copy

Display help
$ help shopt
copy

SYNOPSIS

shopt [-pqsu] [-o] [optname ...]

PARAMETERS

-p
    Display all shell options and their values.

-q
    Quiet mode; return a zero exit status if optname is set, non-zero otherwise. If no optname is given, return zero if all shell options are set, non-zero otherwise.

-s
    Enable (set) the specified shell option(s).

-u
    Disable (unset) the specified shell option(s).

-o
    Restrict the values of optname to those defined for use with the set -o option.

optname
    The name of a shell option to query, set, or unset. If no optname is specified, all options and their settings are listed.

DESCRIPTION

The shopt command in Linux is a built-in shell command, primarily used in Bash, that allows you to set and unset various shell options. These options modify the behavior of the shell, influencing how commands are executed, how file names are expanded, and more. It's a powerful tool for customizing your shell environment to suit your specific needs. You can view the current setting of shell options, enable options to activate specific features, or disable options to revert to the default behavior. Using shopt can significantly enhance your shell scripting capabilities and improve the overall efficiency of your workflow. Many options affect scripting much more than interactive shell behavior.
Understanding how shopt works is essential for any serious Bash user. Using shopt -s and shopt -u you can alter the shell settings, and with shopt alone it outputs a list of the current settings.

CAVEATS

The specific options available through shopt can vary slightly depending on the Bash version you are using. Some options are specific to certain operating systems or distributions.

COMMONLY USED OPTIONS

Some frequently used shell options include:
autocd: Automatically change directory if a directory name is entered.
cdspell: Correct minor errors in directory names when using the `cd` command.
histignore: Exclude specified commands from the history list.
nocaseglob: Enable case-insensitive filename matching.
sourcepath: If set, bash uses the value of the PATH variable to search for the directory containing the file specified as the argument to the source builtin.

EXIT STATUS

When used with the -q option, shopt returns an exit status of 0 if the specified option is set, and a non-zero status otherwise. Without -q, the exit status is 0 unless an invalid option is specified.

HISTORY

The shopt command was introduced as part of the Bash shell, designed to provide a more structured and manageable way to control shell options compared to directly manipulating the `set` command. Its introduction facilitated easier scripting and customization of shell behavior.

SEE ALSO

set(1), bash(1)

Copied to clipboard