Some fun with command line

The commandline in linux can shock newcomers if the usual way via a graphical user interface isn’t possible. Sometimes it might be possible, that you found an awesome command from the Internet. But after you send the command, you realize that you forgot a small but effective sudo command. I recently found a great script for the command line.

What does it make exactly?
Very easily! Once you have sent a command without sudo and you received the feedback that you don’t have administrative permission, the user enters something like “doitanyway” and confirm the entry. Even the previous command is provided with a sudo and now runs correctly.

I describe how it exactly works:

First of all open the .bashrc in your home directory:

nano .bashrc

Now add at the end of this file the following code:

doitanyway ()
{
    set : `history 2 | head -1`;
    shift;
    shift;
    sudo /bin/sh -c "$*"
}

Of course you can also rename to something more meaningful.
Now you save it via CTRL+O and exit with CTRL+X

The changes must be read in by the system with the following command:

source .bashrc

Now you can try your result!
For me it looks like this 😉

doitanyway

How do you rename the command? Am looking forward to your answers

4 comments

  • Nice idea, thanks! I call it “sudoit”.

  • You can also do “sudo !!” to accomplish the same thing.

  • Stephen Angelico

    You don’t need sudo to edit .bashrc (not your own, anyway, and I don’t see how editing someone else’s is beneficial).
    And as commented on Google+, making an alias is a much cleaner way to accomplish this.

  • @Stephen:
    You are absolutely right 😉 I fixed that part with sudo nano .bashrc

    And yes, there are different ways to accomplish this. This tutorial is just a way to show, what is possible 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

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