I'm currently confused why shell globbing in terminal works with negation but shows an error when running in bash.
Take the commands executed in the terminal below, which shows all js files within the
$ shopt -s globstar
$ ls ./HTML/**/!(*.bundle).js
shopt -s globstar
$ bash list-js.sh
list-js.sh: line 4: syntax error near unexpected token `('
list-js.sh: line 4: `ls ./HTML/**/!(*.bundle).js'
globstar only enables the
** pattern. The
extglob option allows
!(...). Somewhere in your interactive shell, that has already been enabled (perhaps in your
.bashrc, perhaps you typed
shopt -s extglob earlier). However, it needs to be enabled explicitly in your script, since such settings are not inherited from the shell that starts the script.
#!/usr/bin/env bash shopt -s globstar extglob ls ./HTML/**/!(*.bundle).js
(As an aside,
globstar does not cause a syntax error because it is treated simply as two adjacent
*s, the second one being redundant.)