shopt

Set and unset bash-style shell options.

Source: src/scripting/shell_options.f90:245-280

Synopsis

shopt [-pqsu] [optname ...]

Description

The shopt builtin manages bash-specific shell options that control globbing, pattern matching, and other behaviors.

Options

OptionDescription
-sEnable (set) option
-uDisable (unset) option
-pPrint in reusable format
-qQuiet mode (exit status only)

Supported Options

OptionDescription
nullglobPatterns matching nothing expand to empty
failglobPatterns matching nothing cause error
globstar** matches directories recursively
nocaseglobCase-insensitive globbing
nocasematchCase-insensitive pattern matching
extglobExtended glob patterns
dotglobInclude dotfiles in glob patterns

Usage

Enable Option

shopt -s nullglob
shopt -s extglob globstar

Disable Option

shopt -u nullglob

Check Option

shopt -q nullglob && echo "nullglob is on"

Show Options

shopt              # Show all options
shopt -p           # Reusable format
shopt globstar     # Show specific option

Option Details

nullglob

Without: Pattern expands to literal if no matches With: Pattern expands to nothing if no matches

shopt -s nullglob
for f in *.nonexistent; do
    echo "$f"      # Loop doesn't execute
done

failglob

Pattern matching nothing causes error:

shopt -s failglob
ls *.nonexistent   # Error: no match

globstar

Recursive matching with **:

shopt -s globstar
ls **/*.txt        # All .txt files in all subdirectories

dotglob

Include hidden files in globs:

shopt -s dotglob
ls *               # Includes .hidden files

extglob

Extended pattern matching:

shopt -s extglob

# Extended patterns:
# ?(pattern)  - Zero or one
# *(pattern)  - Zero or more
# +(pattern)  - One or more
# @(pattern)  - Exactly one
# !(pattern)  - Anything except

ls *.@(txt|md)     # .txt or .md files
ls !(*.bak)        # Everything except .bak

nocaseglob

Case-insensitive file matching:

shopt -s nocaseglob
ls *.TXT           # Matches .txt, .TXT, .Txt

nocasematch

Case-insensitive pattern matching in [[ and case:

shopt -s nocasematch
[[ "HELLO" == hello ]] && echo "match"    # Matches
case "YES" in
    yes) echo "matched" ;;
esac

Examples

Safe Glob Handling

shopt -s nullglob
files=(*.txt)
if [[ ${#files[@]} -eq 0 ]]; then
    echo "No .txt files found"
fi

Recursive File Processing

shopt -s globstar
for file in **/*.py; do
    python -m py_compile "$file"
done

Case-Insensitive Matching

shopt -s nocasematch
read -p "Continue? [y/n] " answer
if [[ "$answer" == y ]]; then
    echo "Continuing..."
fi

Configuration

Add to ~/.fortshrc:

# Enable useful options
shopt -s globstar      # ** recursive matching
shopt -s nullglob      # Empty glob = no match
shopt -s extglob       # Extended patterns
shopt -s nocasematch   # Case-insensitive matching

Exit Status

StatusCondition
0Success, or option is set (with -q)
1Option not set (with -q), or invalid option

Differences from set

setshopt
POSIX optionsBash extensions
-o syntaxDirect option names
$- reflectsNot in $-

See Also