Super User is a question and answer site for computer enthusiasts and power users. 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

How do I repeat the last bash command with some text pre-pended to the front of it.

So if the previous command was:

$ ls /somedir

I want to run the following command utilizing the bash history to prevent retyping / copy & pasting:

$ sudo /somedir

Bonus, how about appending?

share|improve this question
up vote 3 down vote accepted

For sudo you can re-run the same command as above by using the double bang syntax.

ls /somedir
sudo !!

will run sudo ls /somedir

If you just want to use the same path then use !$

ls /somedir
ls -l !$

will run ls -l /somedir

Finally you can re-run commands from history by using ctrl+r to run a reverse-i-search of your bash history.

share|improve this answer

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.