2024-10-21
shopt
shopt
stands for “shell options.” It’s a built-in command in Bash (and other shells like Zsh) that manages shell options, which are variables that determine how the shell behaves. These options affect various aspects, from how file name completion works to how the shell handles errors.
The fundamental usage of shopt
involves three main actions:
shopt -s <option>
to enable an option.shopt -u <option>
to disable an option.shopt -p <option>
to check if an option is enabled and display its current status. You can also use shopt -q <option>
which simply returns a success code (0) if the option is set and 1 if not; useful for scripting.Let’s look at some commonly used shopt
options with practical examples.
1. cdspell
: This option helps prevent typos when using the cd
command. If you misspell a directory name, cdspell
attempts to correct it based on similar directory names.
shopt -s cdspell
cd mydocumnets #Typo!
pwd # Displays the corrected path
shopt -u cdspell
2. dotglob
: This option controls whether files and directories starting with a dot (.) are included in file globbing (using wildcards like *).
shopt -s dotglob
ls -l *
shopt -u dotglob
ls -l *
3. expand_aliases
: Determines if aliases are expanded before other word expansions (such as globbing).
#Enable expand_aliases
shopt -s expand_aliases
alias la='ls -la'
la *
shopt -u expand_aliases
la * #Alias does not expand. This is less intuitive!
4. histverify
: This option enables history verification, prompting for confirmation before executing a command from the history.
#Enable histverify
shopt -s histverify
#Disable histverify
shopt -u histverify
5. nullglob
: This option changes how the shell handles wildcard expansions that do not match any files. By default, if a glob pattern doesn’t match anything, the pattern itself remains. With nullglob
, the pattern is replaced with nothing, avoiding errors in scripts.
ls non_existent_file* # Outputs "non_existent_file*"
shopt -s nullglob
ls non_existent_file* # Outputs nothing
shopt -u nullglob
6. Checking options:
To check the status of an option, use shopt -p <option>
:
shopt -p nullglob # Shows the current state of the nullglob option.
or for just a return code indicating the state:
shopt -q nullglob #Returns 0 if set, 1 if unset.
These examples illustrate the power and versatility of the shopt
command. By understanding and utilizing these options, you can customize your shell environment to improve productivity and streamline your workflow. Experiment with different options to tailor your shell to your preferences and needs. Remember to consult your shell’s manual page (man shopt
) for a complete list of available options and their descriptions.