I have found that setting the extglob shell option only within a compound compound results in failure of subsequent extended globs. Are shell options required to be set outside of compound commands? I did not see an indication of such a requirement in the Bash man pages.
As an example, the following script works fine (prints a.0 a.1):
#!/bin/bash
touch a.0 a.1 \
a.b.0 a.b.1
shopt -s extglob
ls "a."!(b*)
However, if the last two lines are executed as a compound command, the script fails with the following error:
syntax error near unexpected token `('
` ls "a."!(b*)'
This was tested using Bash versions from 4.2 to 4.4 and with a variety of compound commands:
(1) conditional -- if
#!/bin/bash
touch a.0 a.1 \
a.b.0 a.b.1
if true; then
shopt -s extglob
ls "a."!(b*)
fi
(2) braces -- { }
#!/bin/bash
touch a.0 a.1 \
a.b.0 a.b.1
{
shopt -s extglob
ls "a."!(b*)
}
(3) subshell -- ( ):
#!/bin/bash
touch a.0 a.1 \
a.b.0 a.b.1
(
shopt -s extglob
ls "a."!(b*)
)
In all cases, if the shopt is moved outside the compound command, the script succeeds.