
shopt Command in Linux
The shopt command, short for "shell options," is a built-in Bash command that allows users to toggle and view the state of shell options. These options control various features and behaviors of the Bash shell, providing flexibility to customize the shell environment according to specific needs.
The command is especially useful for managing options that affect scripting behaviors, such as error handling, command history, and globbing (pattern matching for filenames).
Table of Contents
Here is a comprehensive guide to the options available with the shopt command â
Syntax of shopt Command
The syntax for the shopt command is straightforward −
shopt [options] [shell_option]
- [options] − Flags that modify how the shopt command behaves.
- [shell_option] − The specific shell option you want to enable, disable, or check.
shopt Command Options
The command shopt provides control over various shell behaviors. Some commonly used shell options include −
Option | Description |
---|---|
-o | Restricts the shell options displayed to only those that align with the options supported by the built-in set command. |
-p | Displays all the current shell options and their states in a user-friendly format, enabling quick review and analysis of your shell's configuration. |
-q | Activates quiet mode where no output is generated. For a single option, it returns a status of TRUE (0) if the option is active, or FALSE (non-zero) if not. For multiple options, it only returns TRUE if all are active, otherwise FALSE. |
-s | Activates (or "sets") the specified shell options. If no options are specified, it displays all the shell options that are currently set. |
-u | Deactivates (or "unsets") the specified shell options, effectively turning them off. |
Examples of shopt Command in Linux
Let's look at some practical examples of the Linux shopt command −
- Viewing set-Compatible Options
- Printing Current Shell Options
- Quietly Checking the Status of an Option
- Enabling a Shell Option
- Disabling a Shell Option
Viewing set-Compatible Options
To restrict the displayed shell options to only those compatible with the set command, use −
shopt -o
This command lists only the shell options that correspond with the built-in set command, streamlining your view for options related to environment and error handling.

Printing Current Shell Options
If you want to see all the current shell options and their states, run −
shopt -p
This command provides a clear output of every shell option, showing whether each option is enabled or disabled, making it handy for diagnosing the shell environment.

Quietly Checking the Status of an Option
To silently verify whether a specific shell option, such as extglob, is enabled, you can execute −
shopt -q extglob && echo "extglob is enabled" || echo "extglob is disabled"
In this case, the command runs in quiet mode and produces no output from shopt itself. However, the exit status is checked, allowing you to echo a message based on the result.

Enabling a Shell Option
If you need to activate a shell option, such as dotglob to include hidden files in globbing results, use −
shopt -s dotglob
Here, the -s option sets dotglob as active, altering the behavior of commands like ls or cp to include hidden files in their results.

Disabling a Shell Option
To turn off a shell option that was previously enabled, such as dotglob, run −
shopt -u dotglob
This command disables dotglob, reverting the globbing behavior to exclude hidden files unless explicitly specified.

Conclusion
The shopt command in Linux is a vital asset for personalizing your Bash shell experience. It provides an array of options to set, unset, list, or silently check different shell features, allowing users to fine-tune their environment for both scripting and interactive sessions.
By using options such as -o, -p, -q, -s, and -u, you can efficiently manage your shell settings, streamlining your workflow and boosting productivity. Becoming proficient with the shopt utility offers enhanced control over your Linux environment, ensuring a more seamless and tailored experience.