Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
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

Doesn't work for me in bash:

$ whoami
$ ^^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
$ sudo !!
sudo whoami
share|improve this answer

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
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

Your Answer


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.