Pattern works in interactive shell but not in script

85 Views Asked by At

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?

1

There are 1 best solutions below

2
On

From Bash manual page:

If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized.

So just add shopt -s extglob to the beginning your e.sh script, since it does not inherit that non-default option from your interactive session.