Server Fault is a question and answer site for system and network administrators. 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

For example, in bash I can type !xyz which will run the last command I typed beginning with xyz.

How can I bring up the last command I typed beginning with xyz but without executing it?
e.g so I can change parameters before execution....

share|improve this question
up vote 19 down vote accepted

You can do it like this: On the command prompt press Ctrl+r and then type the command you want to recall, in your case xyz. This will show you the complete command without executing it.

share|improve this answer
    
Sorry the Control got lost in posting. It should have said "press Control-r" – user2249251 Mar 8 at 7:55
4  
If you want to have a < in your text then you need to use &lt;. Why not use <kbd> syntax (as I edited), that's what it's there for. – Iain Mar 8 at 8:03
1  
More useful than ! notation, because you can press Ctrl-r multiple times to cycle further into matching history. When you arrive at the proper place, you can try to execute not with Enter but with Ctrl-o for much profit. – kubanczyk Mar 8 at 12:27

Try !xyz:p; it will recall the command to the top of history without executing it, so eg up-arrow can immediately access it for interactive editing.

As an aside, I make a practice of doing this when recalling commands with a regexp that could conceivably dig up something damaging, ever since a hasty !r, intended to recall the most recent rsync ... command, pulled back and executed an rm * that had been pushed to the stack more recently than the rsync.

share|improve this answer
    
Thanks, thats a good practice. I have done the same with a rm -rf ./* never been so grateful for a period. FYI The !xyz:p worked, thankyou, but required a few extra keystrokes than the ctrl-r solution. – Mtl Dev Mar 8 at 8:22
    
@MtlDev yes; I find the ^R solution works best for EMACSey folks, whereas the !-path works best for violators like me. – MadHatter Mar 8 at 8:33
  1. Execute the following command. It will echo the last executed command. you can copy and edit the command. This command tested in debian.

    history |tail -2 | grep -v history | cut -d ' ' -f4-

  2. Or Press and hold ctrl+R and type the starting letter of your last command. It will list the commands you executed previously based on your input. Once it shows desired command, just press right arrow in order to select that command.

share|improve this answer
1  
The first command is so smooth !! I think it's faster to just look after the command in history than writing all of this ^__^ – Dex' ter Mar 9 at 7:26

If reverse history search failed, do anything of:

$ history | grep WHAT_YOU_LOOK_FOR

Or:

$ grep WHAT_YOU_LOOK_FOR ~/.bash_history
share|improve this answer

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.