Whenever I need to type a long command I find that the exclamation mark to refer to history very handy. For example,
!ssh can be used to invoke the last
ssh command I used. However I cannot get it to work when space-delimited arguments are added to the search.
!ssh -i, I expect to the last command starting with the string "ssh -i" to be executed. However, the actual command that is invoked is the last
ssh command with whatever argument, and then "-i" was appended to the command.
I can use
!?ssh -i? but that is meant to do something different and I believe there should be a simple way to achieve this. I am also reluctant to type
history|grep ... every time.
So how can I refer to a command in history that starts with a string containing space, as stated above?
Edit It seems that I have not explained the question clear enough. Let's say I have the following history
ssh -i identity host1 echo ssh -i ssh host2
now if I run
!ssh, the third command, namely
ssh host2 will be executed. I expect to type
!ssh -i to get the first command executed, but
ssh host2 -i is executed instead (which results in an error). I can use
!?ssh -i? to execute the first command only if the second command doesn't exist.
ssh -iinstead? I never use history expansion, but always history search, because (a) it always works the same way and (b) it keeps me from mistakes because I can see the line to execute before I really do it and (c) I can do
set +Hby default, so the bad implementation of
bashhistory expansion won't mess up my scripts.
echo ssh -iline to exclude using Ctrl-R!