4

I know that i can enable/disable globbing with set +f and set -f.

But how can i test whether it is currently enabled?

I could create a file with a unique name and test if a file exists with a pattern that should match it. However, I hope there is a cleaner solution.

13

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
0
7

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
4
  • test -o is non-standard, so it would be shell-specific.
    – Kusalananda
    May 16 '20 at 15:43
  • Yeah, I didn't check but I was afraid of that.
    – Iguananaut
    May 16 '20 at 16:14
  • 1
    Well, the question is tagged with bash, so...
    – Kusalananda
    May 16 '20 at 16:48
  • 2
    @Kusalananda That -o option to test comes from ksh, like the corresponding option to set and to the shell itself. set -o was in ksh86 already, but not test -o (which appeared in ksh88). I suppose, like the unary -a, it was not specified by POSIX because it conflicts with the binary -o. zsh supports [[ -o ]] (there's no conflict with binary -o there), but not [ -o ]. May 17 '20 at 10:34
5

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
4
  • Why the single quote around 'f' in your case switch ?
    – Cbhihe
    May 16 '20 at 15:32
  • The '(' is permitted by POSIX but not needed. In 1990, people believed it is needed for $(cmd) but this was wrong. Shells that require it are broken anyway, so there is no need to use it in examples.
    – schily
    May 16 '20 at 21:21
  • @Cbhihe Habit. Quote everything that is fixed to make it clear that it is fixed.
    – icarus
    May 17 '20 at 6:40
  • The extra optional ( helps editors as well.
    – icarus
    May 17 '20 at 6:41
3

There are several ways.

  • The most portable test is expanding $-

    set -f; [ -z "${-##*f*}" ] && echo noglob set
    
  • In bash and a few shells:

    [ -o noglob ] && echo noglob
    
  • A test for a few shells is:

    set -o | grep noglob
    
  • The one meant for bash only is:

    $ shopt -op noglob
    shopt -o noglob               # +o if globbing is active.
    
  • And, if you need a silent test:

    if shopt -oq noglob; then echo noglob set; fi
    
  • You can also test by what globbing does (quite portable):

    set -- .*; if [ "$#" -gt 1 ]; then echo glob active; fi
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.