Set and Unset shell option with shopt command in Linux
shopt command is a shell builtin in Bash (Bourne Again Shell) and other Bash-like shells. It allows users to set and unset shell options that affect the behavior of the shell and the commands it executes. The
shopt command is often used in shell scripts to modify the behavior of the script or to enable certain features that may not be enabled by default.
shopt: shopt [-pqsu] [-o] [optname ...]
Change the setting of each shell option OPTNAME. Without any option arguments, list each supplied OPTNAME, or all shell options if no OPTNAMEs are given, with an indication of whether or not each is set.
|restrict OPTNAMEs to those defined for use with `
|print each shell option with an indication of its status
|enable (set) each OPTNAME
|disable (unset) each OPTNAME
- Returns success if
OPTNAME is enabled.
- Fails if an invalid option is given or
OPTNAME is disabled.
Example usage of
Here are some examples of using the
- To enable the
cdable_vars option, which allows the use of variables as arguments to the
cd command, use the following command:
$ shopt -s cdable_vars
- To disable the
cdable_vars option, use the following command:
$ shopt -u cdable_vars
- To display a list of all the options that are currently set, use the following command:
This will display a list of all the options and their current values.
shopt command is a useful tool for modifying the behavior of the shell and the commands it runs. It can be used in shell scripts to enable or disable certain features, or to customise the behavior of the script.