Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In bash I can use !! to indicate the last command. In ksh I think you can use r to do something similar, but it seems to fail in one of my common patterns: typing sudo [last command].

> cp foo /bar
cp: permission denied
> r c       # If I do this it will repeat my cp call
> sudo r c  # when I do this, it tells me that "r" is not a command

How can I use history to quickly type "sudo [last command]"?

share|improve this question

6 Answers 6

I would use command line edition: type Up and Home (or Ctrl+P then Ctrl+A, or some other sequence in vi mode), then enter sudo␣ and press Return.

share|improve this answer

mksh ships this default alias in its example .mkshrc file since some time already:

alias doch='sudo mksh -c "$(fc -ln -1)"'

Here, “doch” is a pun only German speakers may understand, so let me attempt to explain: some command tells you you aren’t allowed to do something, which leads to a yes-no-yes-no situation, except we have a specific word for this in German. So, this alias is a response “nee-nee, I am allowed to do it!” to the error message above. English Language Learners has got something on this, too, in case I’m too confusing…

But don’t let the naming of this alias restrict you ☺

share|improve this answer

Nicest way I can do this is to make an autoloaded function, say sudo_r:

function sudo_r {
  typeset comm=$(hist -nl ${@:- -1 -1} | sed 1q);
  print -- $comm;
  sudo $comm;
}

Then (say) "alias ss=sudo_r". When this is done, you can type ss cp to repeat the last command beginning with cp, but prefixing it with sudo. Or if you type ss Return, it will sudo the previous command.

share|improve this answer

I think the easiest way is to create an alias like this:

alias ss='sudo $(fc -r -l -n 1 1)

Example:

$ mkdir /test
mkdir: /test: Permission denied
$ ss
$ test -d /test && echo ok
ok
share|improve this answer

Its been a while since I've used ksh but I believe the command was recall typically aliased to r

In bash you can recall the last command that began with any text by entering the text and then ESC CTRL-e, this is advantageous over other ! style history commands as it simply 'recalls' the command without executing it, hence giving one a chance to edit the text before executing

$ sudu <ESC><CTRL>-e

recalls the last command that began with sudo onto the command line, to be edited before execution

share|improve this answer

I use r #number to repeat a command from history. i.e.

dd762c1n4 /tmp 245$ history
245     history
dd762c1n4 /tmp 246$


dd762c1n4 /tmp 246$ r 245
history
245     history
246     history
dd762c1n4 /tmp 247$
share|improve this answer
3  
Please add some details on what is that r and which package contains it. Your answer is not really helpful in this stage. –  manatwork Oct 26 '12 at 18:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.