Pattern match does not work in bash script Pattern match does not work in bash script linux linux

Pattern match does not work in bash script


The extended glob syntax you are trying to use is turned off by default; you have to enable it separately in each script where you want to use it.

shopt -s extglob

Scripts should not use ls though I imagine you were using it merely as a placeholder here.


Globbing doesn't work that way unless you enable extglob shell opt. Instead, I recommend using find:

find . -maxdepth 1 -not -name '<NAME>' -or -name '<NAME>' -delete

before running this command with -delete ensure the output is correct


Method with default settings and no external procs:

for f in *; do [[ $f =~ ^file[12]$ ]] || echo "$f"; done