Can I do filename pattern matching in a bash script?

Posted by Bob Bowden on Ask Ubuntu See other posts from Ask Ubuntu or by Bob Bowden
Published on 2012-10-25T21:11:29Z Indexed on 2012/10/25 23:16 UTC
Read the original article Hit count: 539

Filed under:
|

Can I do filename pattern matching in a bash script?

"test" is a directory with the following files ...

bob@bob-laptop:~/test$ ls
exclude exclude1 exclude2 include1 include2

from the command line, if I want to exclude some of the files, I can do ...

bob@bob-laptop:~/test$ echo !(exclude*)
include1 include2

but, if I put that command in a script (named exclude) ...

bob@bob-laptop:~/test$ cat exclude
echo !(exclude*)

when I execute it, I get an error ...

bob@bob-laptop:~/test$ ./exclude
./exclude: line 1: syntax error near unexpected token ('
./exclude: line 1:
echo !(exclude*)'

I've tried every (I think) variation of escaping some, all or none of the special characters and I still get an error.

What am I missing here? If I can't do this, would someone please be so kind as to explain why?

© Ask Ubuntu or respective owner

Related posts about bash

Related posts about filename