Restricting our focus exclusively to
bash, in this answer on Stack Overflow it is reported the following.
extglobis a flag used by the parser. Functions, compound commands, &c. are parsed in entirety ahead of execution. Thus,
extglobmust be set before that content is parsed; setting it at execution time but after parse time does not have any effect for previously-parsed content.
This is also why you can't run
shopt -s extglob; ls !(*.txt)as a one-liner (when
extglobis previously unset), but must have a newline between the two commands.
However, this is not true for other shell options. Consider e.g. the following.
$ ls -a . .. .hiddenFile file1 file2 file3 $ shopt dotglob dotglob off $ echo * file1 file2 file3 $ shopt -s dotglob; echo * .hiddenFile file1 file2 file3
Is it documented somewhere which shell options are used by the parser like
extglob and therefore cannot be enabled within a group command that uses them?
shopt page in the
bash manual nothing about the behaviour above seems to be mentioned.