What is the purpose of sudo bang bang (sudo !!
) and when might one need to use it?
Asked
Active
Viewed 2.6k times
1 Answers
129
The bang bang (!!
) command is a shortcut to repeat the previous command you entered in your terminal. This command is very useful when you forget that you need admin rights to make a certain action, and lets you repeat it with super-user rights just by typing
sudo !!
instead of typing arrow-up, scrolling to the beginning of the line, adding sudo
and hitting enter (imagine scrolling through those loooong apt-get
commands). So many seconds gained! Yay!
There are many other bang-commands such as !x
, !?x
, !!:p
and !!*
. This blog post lists them and explains what they are for.

416E64726577
- 2,214
- 2
- 23
- 47

alestanis
- 21,519
- 4
- 48
- 67
-
21Note: `ctrl+a` jumps to the start of the line so you don't have jump back one character at a time. – Chris Seymour Feb 28 '13 at 14:08
-
1@sudo_O Surprisingly many developers don't know that either – alestanis Feb 28 '13 at 14:08
-
5And `ctrl+e` jumps back at the end of the line (and there are many more useful options!) – Giorgio Previtera Feb 28 '13 at 14:13
-
And `ctrl+r` matches last commands used. – fedorqui Feb 28 '13 at 14:56
-
7`home` also jumps to the start of the line. – emi Feb 28 '13 at 16:39
-
Recently I've noticed that `ctrl+a` and `ctrl+e` work pretty much everywhere. Text editors, browsers, etc. – t3hcakeman Feb 28 '13 at 16:41
-
I discovered those less than two months ago! – alestanis Feb 28 '13 at 16:45
-
@sudo_O wat... mind blown. However, in what shells do `HOME` and `END` not work? – Julian H. Lam Mar 06 '13 at 15:57
-
4@JulianH.Lam yes [here](http://www.bigsmoke.us/readline/shortcuts) are all the readline keyboard shortcuts. `HOME` and `END` work for me, depends on shell, set up, ect.. – Chris Seymour Mar 06 '13 at 16:04
-
1@JulianH.Lam terminal for mac home scrolls all the way to the top of the session .. although mac keyboards don't even have home keys .. but I think `fn` and left arrow key is the same – GP89 Mar 08 '13 at 17:15
-
note: this is a bash builtin feature, that you can unset with `set +o histexpand`. – Benoit Nov 18 '13 at 12:25
-
I use `set -o vi`, so `ctrl+a` and `ctrl+e` insert nonsense characters, while `0` and `$` jump to start/end of line. – Kyle Strand May 25 '15 at 21:27
-
2@pythonian29033 what are you trying to say? Answering your own question is highly encouraged – Tim Aug 08 '16 at 12:47
-
I know, I did the same a couple years back lol – pythonian29033 Aug 08 '16 at 13:33
-
@JulianH.Lam in the Windows SSH client PuTTY `HOME` and `END` don't work properly. – luizfls Oct 17 '17 at 16:37
-
Yes... as I've come to realise in the past 4 years, there are other development environments besides my own. My reply up there in 2013 seems awfully small-minded. – Julian H. Lam Oct 17 '17 at 17:21
-
You can also hold down left arrow, ctrl+b, or alt+b to get to the beginning of the line (◠﹏◠) – N7L Jun 15 '18 at 17:44
-
@luizfls HOME and END work for me in Windows SSH PuTTY – mbomb007 Mar 24 '21 at 18:26
-
@mbomb007 I'm SSHing into a CentOS 6 machine and when I hit `HOME` or `END`, what I get is a `~`. – luizfls Mar 24 '21 at 22:12
-
1@luizfls Have you tried [this](https://superuser.com/a/104001/494173)? Or are you using Home and End [on a numpad](https://superuser.com/a/1159876/494173)? There's also a setting in Terminal > Keyboard. – mbomb007 Mar 25 '21 at 19:08
-
@mbomb007 Thanks! The first link did it. :-) – luizfls Mar 25 '21 at 23:30