1

I run a command, then realize it should have been run with sudo.

In zsh I have a function so that I can hit a ^x^x and it will grab the previous line from the history and prepend sudo to it, so I can run it again.

Is it possible to do this with bash?

thermans
  • 237
  • 1
  • 2
  • 6

2 Answers2

2

As mentioned by Gerald Schneider, this is a duplicate that exists on Stack Overflow, but since it doesn't exist on Serverfault, and it won't let me mark it as duplicate, I'll just put the answer here as well.

From jm666 question here


You can write:

sudo !!

(See §9.3 "History Expansion" in the Bash Reference Manual.)


trueCamelType
  • 1,016
  • 5
  • 19
  • 41
0

Most bash implementations I have used allow command-history navigation via the up- and down-arrow keys.

So in addition to @trueCamelType's solution, one could also use:

<up-arrow><home>sudo<space><enter>

Eight characters, either way. :)

Jim L.
  • 645
  • 4
  • 10