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?

1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.