How can I use the bash history to run a command with the arguments from a command in the history?

I know that I can use !! to rerun the last command, but how can I run a new command with those arguments?

eg, I first run:

~/somefolder/script.sh somefile

I want to quickly run

~/somefolder/script.sh anotherfile

I tried !! anotherfile but it didn't work.

Or even better, how can I run !-2 newarguments to run the second last history command with new arguments?

I don't want to use the arrows to scroll through the history then backspace over the arguments.


To pick the command name, use the :0 modifier:

$ man bash
$ !!:0 csh
man csh

Chose all arguments with most recent command with !*

$ man sed awk perl
$ which !*
which sed awk perl

Choose the 2nd command previously executed relative to the current command, and pick all its arguments with:

$ echo !-2:*
echo sed awk perl
sed awk perl

Choose the 3rd command previously executed relative to the current command, and pick its 2nd argument:

$ echo !-3:2
echo awk

Choose the last command that began with "which" and plug in its 3rd argument:

$ echo !which:3
echo perl

All that said, the answers to your two specific questions:

  1. See the first example, above.
  2. !-2:0 "new list of arguments"
  • Read the BASh manual. ;-) Just know that you first select a line, perform substitutions, and then refine that with modifiers, then maybe just print the result :p. Explore matching lines with !?text?, or match an occurrence of the text anywhere in a history command's line to match the line. Other variants match a line, then don't just grab pieces of the line, but perform a regular expression replacement before executing the command i.e. !wh:gs/sed/tr/, which command subs for sed, tr. – Billy McCloskey Jan 14 '14 at 22:45

If you press the keyboard shortcut Ctrl-R in the terminal, and type any unique portion of the command, it does reverse search and finds it for you. No need to scroll all the way.

  • Yes, I knew about Ctrl-R, but you still have to backspace to remove the arguments, which I specified in the question that I didn't want to do. Thanks for the answer anyway. BTW I wasn't the one who downvoted you. – localhost Jan 14 '14 at 7:01
  • @localhost If an answer is not useful for your question, you should downvote it. That's how the system works; you wouldn't have to excuse yourself for it. – slhck Jan 14 '14 at 7:25
  • @slhck Possibly true to the letter of the law, but it seems a bit rude to downvote someone for making an honest effort. If I had not known about Ctrl-R, then jadelord's answer would still have helped me somewhat. Your point is well taken though. – localhost Jan 14 '14 at 7:31
  • It's an alternative, for the uninitiated. That's all. It's OK @localhost, and thanks. – jadelord Jan 16 '14 at 15:07

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.