Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The command works fine on Linux but on OS X Mountain Lion when I'm doing:

rm !(myfile)

bash complains:

bash: !: event not found

Is there any way to do it the same on OS X?

share|improve this question
    
What are you trying to do, remove a file named !(myfile) ? Or... ? – Kyle Jones Jan 11 '13 at 22:48
up vote 2 down vote accepted

This is because you haven't set the extglob option, which gives a special meaning to !.

In your Bash, write:

shopt -s extglob

Then, the command should run as expected.

By default, ! expands to the process ID of the most recently executed background (asynchronous) command. With the extglob set, ! can be used for pattern matching, but only in conjunction with parentheses following it ().

You could of course add this line to your ~/.bash_profile to have it set any time you open Terminal.app. OS X by default runs a login shell, so these options should be set in ~/.bash_profile and not ~/.bashrc as you're probably used to from Linux terminal emulators.

share|improve this answer
    
"!" has special meaning for command history, extglob enables its use for negative pattern matching which can override that. The error observed is due to the history feature (a minor irritation being that the errant command is not added to your command history). – mr.spuratic Jan 11 '13 at 22:58
    
The pid of the last background process is $!. Just ! isn't expanded to anything by default by bash. – user495470 Jan 12 '13 at 9:38
    
@Lauri, yes, in combination with $, as explained in the Bash reference manual. Maybe that part wasn't clear. – slhck Jan 12 '13 at 10:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .