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.

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

Is there an easy way to re-apply a previous command to a new command line entry?

Say I typed in chmod u+r,g+x file.txt but forgot the sudo. Could I simply type sudo <some easy symbol>'?

Pardon for the possible noob question.

share|improve this question

migrated from serverfault.com Jul 28 '14 at 16:52

This question came from our site for system and network administrators.

Have you tried using your arrow keys? – Michael Hampton Jul 28 '14 at 16:19
Yes, looking more if I have an enter long line, or say I need to plug the previous command in between a new command. – tdm Jul 28 '14 at 16:21
See also Execute a line of commands with one sudo – Gilles Jul 28 '14 at 22:31
up vote 5 down vote accepted

You can do:

sudo !!

Another good one is alt ., to insert the last parameter of the previous command

share|improve this answer
Perfect, this is exactly what I was looking for you. Thanks. – tdm Jul 28 '14 at 16:22
Then 'accept' the answer :) – Halfgaar Jul 28 '14 at 16:22
Waiting too. Another 7 min. :) – tdm Jul 28 '14 at 16:25
This only works in simple cases though, not when the command is a complex one involving redirections, pipes, etc. – Gilles Jul 28 '14 at 22:31

Press up arrow, press ctrl+a, write sudo, press enter.

share|improve this answer

There are some basic bash shortcuts you should know...

Ctrl + A    Go to the beginning of the line you are currently typing on
Ctrl + E    Go to the end of the line you are currently typing on
Ctrl + L    Clears the Screen.
Ctrl + U    Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H    Same as backspace
Ctrl + R    Let’s you search through previously used commands
Ctrl + D    Exit the current shell
Ctrl + W    Delete the word before the cursor
Ctrl + K    Clear the line after the cursor
Ctrl + T    Swap the last two characters before the cursor
Esc + T     Swap the last two words before the cursor
Alt + F     Move cursor forward one word on the current line
Alt + B     Move cursor backward one word on the current line
Tab         Auto-complete files and folder names

In your specific case, I also alias h to history|grep.

Such that:

# mco service sendmail status -F operatingsystemmajrelease=6

And I need to prepend something to that...

# h mco

Which shows...

  114  07-28-2014 09:33:25  mco package sendmail install -F operatingsystemmajrelease=6
  115  07-28-2014 09:33:25  mco service sendmail status -F operatingsystemmajrelease=6
  116  07-28-2014 09:33:25  mco package sendmail-cf install -F operatingsystemmajrelease=6

And I want line #116... So I type:

# !115

But if I need something in front of it (e.g. sudo), I'd do...

# sudo !115
share|improve this answer
As I mentioned in my post, alt . is an important one too. And might as well add alt / too, to circumvent smart tab completion and it the old fashioned way. Or alt *, to expand a *. – Halfgaar Jul 28 '14 at 19:17

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.