5

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?

  • 1
    +(a|b) shouldn't be 'quoted' in your script; that suppresses evaluation. – mrb Aug 15 '12 at 13:28
8

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.