CMCDragonkai
CMCDragonkai

Reputation: 6372

Using brackets/parantheses inside shell scripts vs using them from the command line bash

When I use rm -r secrets/!(snapsearch) on the command line. It works and deletes everything in the secrets folder except for snapsearch folder.

However when I use the exact same command in a bash script and execute it, it complains about the parentheses.

syntax error near unexpected token `('

Why does it work from the command line, and not from the bash script? And how can I make this work from the bash script?

Upvotes: 1

Views: 354

Answers (1)

devnull
devnull

Reputation: 123448

You need to enable extglob in non-interactive mode, i.e. when executing your script.

You have two options. First, add the following line to the top of your script:

shopt -s extglob

Note that since extglob changes the way in which expressions are parsed, so it needs to be on a separate line by itself and cannot be a part of a block, e.g. a if block.

Second, execute the script by saying:

bash -O extglob scriptname

(The second option wouldn't require changes to the script.)

Upvotes: 1

Related Questions