Glob options let Bash decide how wildcard patterns behave before a command receives its arguments. They matter when a script must handle empty match sets, hidden files, or nested directories without accidentally processing a literal pattern such as empty/*.log.

Bash controls these behaviors with the shopt builtin. By default, an unmatched glob stays as literal text; nullglob removes it, dotglob lets a wildcard match names beginning with a dot, and globstar makes ** walk into subdirectories. Use failglob instead of nullglob when an unmatched pattern should stop the command with an error.

Set glob options close to the array assignment or loop that needs them. The setting belongs to the current shell process, so a script started with bash script.sh exits with its option state, while options enabled in an interactive shell remain active until shopt -u disables them.

Steps to use Bash glob options:

  1. Create a small test tree with visible, hidden, nested, and empty match cases.
    $ mkdir -p reports/archive empty
    $ printf 'app\n' > reports/app.log
    $ printf 'db\n' > reports/archive/db.log
    $ printf 'hidden\n' > reports/.hidden.log
    $ printf 'note\n' > reports/readme.txt
  2. Create the glob option demo script.
    glob-options-demo.sh
    #!/usr/bin/env bash
    set -euo pipefail
     
    without_null=(empty/*.log)
    printf "without nullglob:\n"
    printf "count=%s\n" "${#without_null[@]}"
    printf "value=%s\n" "${without_null[0]}"
     
    shopt -s nullglob
    with_null=(empty/*.log)
    printf "with nullglob:\n"
    printf "count=%s\n" "${#with_null[@]}"
     
    shopt -s dotglob
    dot_matches=(reports/*.log)
    printf "with dotglob:\n"
    printf "%s\n" "${dot_matches[@]}"
     
    shopt -s globstar
    recursive_matches=(reports/**/*.log)
    printf "with globstar:\n"
    printf "%s\n" "${recursive_matches[@]}"

    Set options before assigning a glob to an array or starting a loop. Bash expands the pattern at that point, not later when the value is printed.

  3. Check the script syntax.
    $ bash -n glob-options-demo.sh

    No output means Bash did not find a parse error.

  4. Run the script and verify each glob option changes the match set.
    $ bash glob-options-demo.sh
    without nullglob:
    count=1
    value=empty/*.log
    with nullglob:
    count=0
    with dotglob:
    reports/.hidden.log
    reports/app.log
    with globstar:
    reports/.hidden.log
    reports/app.log
    reports/archive/db.log
  5. Disable the options when they were enabled in the current interactive shell.
    $ shopt -u nullglob dotglob globstar

    A script run with bash glob-options-demo.sh exits with its own option state, so this cleanup is only needed after interactive testing.

  6. Remove the sample script and directories.
    $ rm -rf glob-options-demo.sh reports empty