r/bashtricks • u/QuantumCow • May 17 '11
Execute previous command as root
If you type out a long command and forget to sudo it, it's a pain to type it out again. Instead:
sudo !!
14
Upvotes
5
u/aperson May 17 '11
From my sticky note on my desktop:
!! - Last command and all arguments
!-3 - Third-to-last command and all arguments
!^ - First argument of last command
!:2 - Second argument of last command
!$ - Last argument of last command
!* - All arguments of the last command
!42 - Expands to the 42nd command in history
!foo - Last command beginning with 'foo'
!?foo - Last command containing 'foo'
^foo^bar - Last command with first instance of 'foo' replaced with 'bar'
!:gs/foo/bar - Last command with all instances of 'foo' replaced with 'bar
<command>:p - Don't execute and print command0
May 17 '11
[deleted]
3
u/ffreire Jun 01 '11
Most terminals follow Emacs key-bindings, i.e. you could use CTRL+A to get to the first character in the string, and CTRL+E to get to the last character in the string. Very handy stuff :]
4
u/Hegzdesimal May 17 '11
You can also use the ! to get at other previous commands from your history file.
is the last command.
You can also use !-n to get to a to get to the previous nth command. !-1 is also the last command !-6 would be the 6th command back in your history.
!n will get you to the nth command down your history file.
would be the 100th command in your history.
will display your history file