Bash – Extended file globbing not working with cat inside bash script

bashshellshell-scriptwildcards

When, in my terminal, I type

cat ~/my/+(a|b)/doc

It reads ok from both ~/my/a/doc and ~/my/b/doc, but when I put that command in a bash script:

#!/bin/bash
cat ~/my/'+(a|b)'/doc

I get the error:

cat: ~/my/+(a|b)/doc: No such file or directory

Is it not possible to use extended globbing inside a bash script?

Best Answer

You have to turn on extglob:

#!/bin/bash
shopt -s extglob
cat ~/my/+(a|b)/doc

+() is an extended pattern, is only recognized when extglob is enabled. By default extglob is on in interactive shells and off in non-interactive shells.

Related Question