If I type rm main!(.tex|.pdf) into a bash terminal, it will delete all files that start with main but do not end in .tex or .pdf, clearing up various junk files that are strewn around by LaTeX typesetters.

The following shell script, however, doesn't do what I expect it to when placed in a file and then run


rm main!(.tex|.pdf)

A syntax error is reported at the ( token. What gives? What's the canonical way of doing something like this as part of a bash script. Is there are more elegant way of doing this than bringin find to bear against the problem?


If the pattern does not work when run from a script, make sure that the extglob option is set for non-interactive use, too.

Just put

#!/usr/bin/env bash
shopt -s extglob

on top of your script after the hashbang. That way it's easy to spot. See also: Glob – Greg's Wiki

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.