Nathan
Nathan

Reputation: 1762

Getting previous line in Bash after new command?

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

Answers (2)

0xC0000022L
0xC0000022L

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

ruakh
ruakh

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

Related Questions