Somewhere around 2020, I learned about the
!! command in bash. This command is a part of history expansion in bash, and it allows you to repeat the last command that was executed. It can be particularly useful when you forget to run a command with the necessary permissions and want to rerun it with, for example,
Imagine you are trying to edit a configuration file and forget to run the command with administrative permissions. Here’s how the ”!!” command can help:
nvim /etc/nginx/conf.d/*.conf # error when saving due to lack of permissions sudo -E !! # This will rerun the previous command with sudo, i.e., 'sudo -E nvim /etc/nginx/conf.d/*.conf'
This can save time and reduce the risk of typing errors. The
!! command can be a handy addition to your bash toolkit!