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!
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With