MacOS – What does rm -rf $(!!) mean on macOS

bashcommand linemacos

I know rm -rf is a force deletion on everything in a given directory. But I don’t understand what rm -rf $(!!) will do on the command line.

How can I look up this syntax or understand what it will do?

Best Answer

  • !! is a history command of bash, it gets replaced by the last command you‘ve executed. You can try ls followed by echo !!
  • $(...) performs Command Substitution, the output of a command replaces the command name (the part within the ()). Again, echo $(!!) helps to illustrate this

So rm $(!!) removes all files whose names are printed after re-running the last command. Watch out for file names containing spaces or special characters like * or ? or ~; it won‘t work for those, or it will remove a different set of files than you’d think.

Given the risks it's probably safer to run echo PATTERN, verify the result and then go back one line in the history, replace echo by rm and execute this. Instead of navigating back you can also just run ^echo^rm^ (which uses history expansion to replace the command used in the line before).