Search code examples
bashrm

syntax error on bash rm -rf !; unexpected token


This command is working perfectly when you just type it into console and run;

rm -rf !(folder1|file_name|log.txt|*.sh|*.conf)

However, when I run it from a bash script, it gives me the following error;

./test.sh: line 1: syntax error near unexpected token `('
./test.sh: line 1: `rm -rf !(folder1|file_name|log.txt|*.sh|*.conf)'

Solution

  • It's the extended glob matching syntax. You need to enable it with shopt -s extglob before the rm command.