Quickly sudo-ing the previous command in Bash Shell

Amit Merchant · May 7, 2021 ·

It’s just another day where I got to learn gemstone of command for Linux’s Bash shell once again.

So, you probably have run into this scenario where you forget to type sudo before your command that requires root privileges. For instance, the apt-get install command.

When you do this, the bash terminal would give you the following error.

So, to run this command with sudo what you would normally do is go back to that command and prepend sudo to it, right? Not anymore.

✨ Enter sudo !!

There’s a better and quick way to do it with just one command. Check it out.

$ sudo !!

What this command will do is it will run the previously run command with sudo so you don’t need to go back to the command and prepend or type the command all the way from scratch.

It’s just a nifty little command that can improve your workflow dramatically.

But be aware that this command only works in Bash-based shell. So, that’s the only caveat you need to know.

👋 Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!