Linux Command Reference ยท Shell Builtins
Bash shopt command
Reference for the Bash shopt builtin: list and toggle options like nullglob, globstar, extglob, dotglob, nocaseglob, histappend, checkwinsize and expand_aliases.
shopt turns Bash-specific behaviors on (-s, "set") and off (-u, "unset"). It complements set -o, which holds the older POSIX-style options. The shopt options mostly change how Bash expands patterns, how history and the prompt behave, and how strict the shell is about a few edge cases.
Run shopt alone to list every option with its current state, or shopt NAME to query one.
shopt [-pqsu] [-o] [OPTNAME ...]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
shopt [-pqsu] [-o] [optname ...]
| Name | Description |
|---|---|
-s |
Enable (set) each named option. |
-u |
Disable (unset) each named option. |
-q |
Quiet: print nothing, only return 0 if all named options are on. |
-p |
Print options as reusable shopt -s/-u name lines. |
-o |
Restrict the names to those accepted by set -o. |
| no names | List all options with on or off. |
Frequently used options:
| Option | Effect when on |
|---|---|
nullglob |
A pattern with no match expands to nothing instead of staying literal. |
failglob |
A pattern with no match is an error. |
dotglob |
* also matches names starting with a dot. |
nocaseglob |
Pattern matching ignores case. |
globstar |
** matches files and directories recursively. |
extglob |
Extended patterns: ?(...), *(...), +(...), @(...), !(...). |
nocasematch |
Case-insensitive case and [[ == ]]/=~ matching. |
histappend |
Append to the history file instead of overwriting it. |
histverify |
Show history-expanded lines for editing before running them. |
cdspell, dirspell |
Fix small typos in cd arguments and directory completion. |
checkwinsize |
Update LINES and COLUMNS after each command. |
expand_aliases |
Expand aliases (on interactively, off in scripts). |
lastpipe |
Run the last stage of a pipeline in the current shell (needs job control off). |
inherit_errexit |
Command substitutions inherit set -e (Bash 4.4 and later). |
autocd |
Typing a directory name acts like cd into it (interactive). |
Returns: 0 if the named options are all enabled (query) or the change worked; non-zero if an option name is invalid or, in query mode, an option is off.
Errors and edge cases: an invalid name prints "invalid shell option name". With no -s or -u, shopt NAME prints state and returns 1 if the option is off, so it can be used in conditions.
Usage notes
- Scope. Options belong to the shell where you set them.
shopt -sin a script does not change your terminal; a subshell starts with a copy of the parent's settings. extglobmust be set before parsing. It affects how Bash parses the line, so enabling it on the same line as the pattern (shopt -s extglob; ls !(x)) does not work; put it on an earlier line, and enable it before defining functions that use extended patterns.nullglobpitfalls. With it on,ls *.zipbecomes plainlsand lists the current directory. Combine with arrays (files=(*.zip)) and check${#files[@]}instead.globstarcost. On large trees**can be slow and follows no symlinked directories in older Bash versions (symlinks were followed before 4.3).- Defaults differ. Some options are on by default in interactive shells (
checkwinsize,expand_aliases,progcomp);shoptwithout names shows what your build uses.bash --posixflips several. - Save and restore.
old=$(shopt -p nullglob)...eval "$old"restores exactly the previous state, whether it was on or off. - Where to put them. Interactive options in
~/.bashrc, script options at the top of the script. shoptis Bash-only. Zsh usessetopt; POSIX shells have none of these options, so scripts using them need#!/bin/bash.
Related
set,alias,history,declare- Lessons: Globbing, Advanced Patterns, Portability