Reputation: 1762
In a Bash terminal, I often type a command and realize that I needed to sudo that command. I hit the up arrow to get the previous command and then backtrack to the beginning type sudo and enter.
Is there any way to type sudo, then press a key to pull down the previous command after sudo?
Thanks!
Upvotes: 3
Views: 395
Reputation: 21339
sudo !!
!!
denotes the previous command
I strongly recommend to visit CommandlineFu which offers a lot of tips and tricks similar to this answer.
One word of caution: if you have HISTIGNORE
set like I do, only those commands not ignored can be invoked again this way.
Upvotes: 2
Reputation: 183582
Yes: you can use "history expansion", and write !!
:
$ foo
bash: foo: command not found
$ sudo !!
sudo foo <-- it prints out the expanded command
bash: sudo: command not found <-- and then runs it
Upvotes: 4