Test whether globbing is enabled

If you do set -f, or otherwise disable globbing:, $- will contain f:

$ echo $-
himBHs
$ set -f
$ echo $-
fhimBHs
$ bash -fc 'echo $-'
fhBc

So:

[[ $- = *f* ]]

Or:

case $- in
 *f*)  ... ;;
esac

In bash at least, I don't know if this is shell-specific, you can use test -o <optname>. The option name corresponding to -f is noglob so you can do something like this:

$ set -f
$ test -o noglob; echo $?
$ set +f
$ test -o noglob; echo $?

or, in a script,

if [ -o noglob ]; then
    echo globbing disabled
else
    echo globbing enabled
fi

The variable $- has the current flags in it.

So you can do

case "$-" in
   (*'f'*) echo "file globbing is disabled" ;;
   (*)     echo "file globbing is enabled" ;;
esac

Tags:

Bash

Wildcards