The glob pattern in bash does not recognize '('

When executing the following command:

rm -rf !(file1|file2)

all files are deleted except file1 and file2; as expected. If either put this command in a bash script:

#!/bin/bash
rm -rf !(file1|file2)

or by running it with bash -c:

bash -c "rm -rf !(file1|file2)"

I get the following error:

syntax error ner unexpected token '('

I tried setting shell parameters using

shopt -s extglob

embedding in:

bash -c "shopt -s extglob; rm -rf !(file1|file2)"

to enable glob according to: https://superuser.com/questions/231718/remove-all-files-except-for-a-few-from-a-folder-in-unix and some other questions.

And yet it does not work, and I am at a loss.

+4
source share
1 answer

, , echo !(file1|file2) rm -rf !(file1|file2).

bash shopt -s extglob. bash (, extglob . .

:

bash -O extglob -c 'echo !(file1|file2)'

script , :

#!/bin/bash
shopt -s extglob
echo !(file1|file2)

-c:

bash -c 'shopt -s extglob
echo !(file1|file2)'

:

bash -c $'shopt -s extglob\necho !(file1|file2)'
+6

All Articles