Getting the previous line in Bash after a new command?

In the Bash terminal, I often type a command and realize that I need to execute that command. I press the up arrow to get the previous command and then go back to the original sudo type and type.

Is there a way to enter sudo then press a key to take out the previous command after sudo?

Thank!

+3


source to share


2 answers


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

      

+4


source


sudo !!

      

!!

denotes the previous command



I highly recommend visiting CommandlineFu , which offers many tips and tricks similar to this answer.

One word of caution: if you have HISTIGNORE

, as I do, only commands that are not ignored can be called again this way.

+2


source







All Articles