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.

Sorry if this is a yes/no question.. I am reading this unix tutorial and found the following:

Repeating and Modifying the Previous Command

[..]
If you mistype leavenworth as leaveworth you can correct it with the following command: % ^leave^leaven

Unfortunately I don't have a computer to try this on, but I was wondering:

Since some commands require sudo to run, could I then write ^^sudo to "insert" sudo to the previous command?

share|improve this question
1  
Get a test computer! You can't make sense of a unix tutorail with somewhere to bang out some commands. Obviously you typed this question up somewhere. If you're stuck on windows get cygwin or ssh into some box somewhere but by all means have a prompt in front of you to test stuff as you ask questions. –  Caleb Apr 28 '11 at 12:15
    
I have a unix system at home but forgot to start it up this morning. I'll start it up tomorrow. Promise –  Default Apr 28 '11 at 12:21
add comment

3 Answers 3

up vote 6 down vote accepted

I think using history completion is a much more universal way to do this

$ sudo !!

Most shells have some shortcut for the previous command. That one works in bash and zsh. There are various ways you can do substitution, but usually these are best left for removing or changing bits, if you want to expand it, just grabbing the whole thing is the simplest way. You can add whatever you like before and after the !! to expand on the previous command.

Edit: The original question was about prepending to the previous command which the above covers nicely. If you want to change something inside it as the commentor below the syntax would go like this:

$ sudo !!:s/search/replace/

...where 'search' is the string to match against and replace...well you get the idea.

share|improve this answer
    
So, to complicate matters, what if I run echo Calen and wanted to instead run sudo echo Caleb.. Could I then do something like.. sudo !!^Calen^Caleb ? –  Default Apr 28 '11 at 11:56
    
The right syntax for that would be sudo !!:s/Calen/Caleb/ –  Caleb Apr 28 '11 at 12:08
    
and that deserves the accepted answer. Thanks Caleb for your help –  Default Apr 28 '11 at 12:15
    
With bash, you can set +o histexpand if you want to turn it off. This is what I do, because any ! inside double quotes will trigger an error (like echo "Hello, World!") –  Benoit Apr 28 '11 at 12:41
add comment

YES. You can do that. e.g. to run the ls command in /root directory, you can do that as:

$ ls
$ ^ ^sudo 

The second command will execute as "sudo ls"

Mind to it that you add one "space" after sudo, in second line.

share|improve this answer
1  
Interesting idea, but that doesn't work either (at least on my system). I don't see how it would either, because there's no space in ls to substitute. –  Mikel Apr 28 '11 at 11:25
    
It did work on my debian system with bash version as > GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu) Copyright (C) 2007 Free Software Foundation, Inc. –  SHW Apr 28 '11 at 11:27
    
Doesn't work for me. I think sudo !! is the better solution here. –  Caleb Apr 28 '11 at 11:47
    
I guess so. I think, >sudo !! is the good answer to this question. –  SHW Apr 28 '11 at 11:48
    
In order for this (as you typed it) to work in most versions of bash, you would also have had to left a space BEFORE ls in the previous command. –  Caleb Apr 28 '11 at 12:18
add comment

Doesn't work for me in bash:

$ whoami
mikel
$ ^^sudo
-bash: :s^^sudo: no previous substitution

or zsh:

$ ^^sudo
$

or even tcsh, which I think invented that syntax:

% ^^sudo
No previous left hand side.

!! does the job:

$ whoami
mikel
$ sudo !!
sudo whoami
root
share|improve this answer
    
worst part is: I read about !! seconds earlier (Although, they didn't show that you could set the !! after anything else). My memory span is waay to short. Thanks for your answer :) –  Default Apr 28 '11 at 11:34
    
@Default: Welcome! "Thanks for your answer :)" is best expressed as a green tick next to the answer that helped you most. FAQ –  Johnsyweb Apr 28 '11 at 11:56
    
@Johnsyweb: thanks, i am aware of how SE works. However, since I see other answers informing me of the exact opposite and since I asked the question less than one hour ago, I do not feel ready to accept an answer yet. A better advice here would be to ask me to at least upvote this answer since upvoting is allowed when reputation exceeds 15. –  Default Apr 28 '11 at 12:05
add comment

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.