Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer

up vote 2 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
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.