Sudo previous command !!

Add Sudo to Your Last Bash Command With “!!” Syntax

How often have you typed in a command in your linux shell, and then realized that you forgot to type sudo, so you end up with an error or editing the dreaded read-only file? This happens to me much more than I’d like to admit, so I’m writing about it.

You have a couple of options, the easiest one is to just use !! to tell bash to use the last command.

For instance, if you tried to type this, you’d get an error that you are editing a read-only file:

vi /etc/apt/sources.list

Once that happens, then you can just use this command:

sudo !!

Which bash will then expand into this command:

sudo vi /etc/apt/sources.list

g33kadmin

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.