The ‘!!’ command of the BASH shell with sudo

April 25 2009 | Bash, Linux | No Comments

It’s good security practice to log in to your Linux system as an under privileged user, only to then use commands like sudo to execute the application that you want with root privileges. But what if you log in using a terminal client that isn’t configured to properly use keys like ‘Home’ or ‘End’? I’ve found this all too often when using a terminal client on the Windows platform. You’ll quickly know if the terminal isn’t set up for it because garbage text will appear inside the terminal screen or maybe even some other random behavior such as the line of text disappearing.

Now, what if you type a really long command into bash through such a terminal client and you forget to prefix it with sudo? You’d probably end up trying to retype the command or do some form of mouse copy-paste magic to get the command to follow sudo. There’s an easy solution to this problem! Instead, you can use a feature of the bash shell, ‘!!‘, to tell bash that you want it to execute your previously given command.

For instance, if you typed:
nano /etc/resolv.conf

But you really meant to type:
sudo nano /etc/resolv.conf

You can accomplish the same thing with:
adam@ad:~$ sudo !!
sudo nano /etc/resolv.conf
[sudo] password for adam:


© 2018 All logos and trademarks are copyright their respective companies.