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?
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 ofbash
, it gets replaced by the last command you‘ve executed. You can tryls
followed byecho !!
$(...)
performs Command Substitution, the output of a command replaces the command name (the part within the()
). Again,echo $(!!)
helps to illustrate thisSo
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, replaceecho
byrm
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).