In my interactive shell I can store a pattern in a variable
$ targets="?(pcm52|pcm61)"
$ ls -l config/devdesc.$targets
-rw-r--r-- 1 kfa kfa 113 Dec 16 13:43 config/devdesc.pcm52
-rw-r--r-- 1 kfa kfa 14 Dec 16 13:44 config/devdesc.pcm61
But if I create a shell script
targets="?(pcm52|pcm61)"
ls -l config/devdesc.$targets
I get this error running it
$ bash -x /tmp/e.sh
+ targets='?(pcm52|pcm61)'
+ ls -l 'config/devdesc.?(pcm52|pcm61)'
ls: cannot access 'config/devdesc.?(pcm52|pcm61)': No such file or directory
Why does it fail when put into a script?
From Bash manual page:
So just add
shopt -s extglob
to the beginning youre.sh
script, since it does not inherit that non-default option from your interactive session.