2009-07-12

Re-issue Previous Command as Root

Here's a quick Bash tip. Don't you just hate it when you key in a command at the terminal and press enter only to realize that you didn't preface the command with sudo? Well, have no fear, because the command !! will re-issue the previous command.

Now, I know you're probably thinking, "what good does that do me? I can just press the up arrow and then hit enter to run the last command." And you'd be right; however, what if you wanted to re-issue the previous command, but you wanted to put sudoat the beginning of it? Well, you could simply press the up arrow and then move the cursor to the beginning of the prompt and then key in sudo and press the enter key, or you could just do this:

$ sudo !!

The time that this happens to me most frequently is when I want to edit a file, for example, if I wanted to add a new repository to my /etc/apt/sources.list file, and run the following by mistake:

$ vim /etc/apt/sources.list

This will open up the sources.list file as read only, a fact which becomes perfectly clear once you change to insert mode in vim, with a Warning: changing a readonly file warning across the bottom of the window. At this point, it's easy to close the file, and re-open it as root with:

$ sudo !!

Well, that pretty much does it for tonight. See you next time.

No comments:

Post a Comment