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.

I know about the bash feature !text, which runs the last command starting with "text". But it seems not to work, when "text" contains space. Sometimes it is useful to search for something, containing spaces.

for example:

history | grep ping
  514  ping 192.168.100.2
  515  ping 192.168.100.3
  516  ping 192.168.100.4
  517  ping google.com
  518  ping 192.168.100.5
  519  ping 192.168.100.6
  520  ping 192.168.100.7

I want to call the line "ping google.com", using the ! feature. I tried some things, but they failed:

this is the standard behavior, it runs the last ping command

/root# !ping  
ping 192.168.100.7

A simple string with space after ! does not work

/root# !ping google
ping 192.168.100.7 google

As I know, quotes tell bash to interpret a string with space as a single string, instead of 2 separate strings. But here it does not work as expected.

/root# !"ping google"
bash: !"ping: event not found

/root# !'ping google'
bash: !'ping: event not found

/root# "!ping google"
"ping 192.168.100.7 google google"

/root# '!ping google'
bash: !ping google: command not found

/root# !\"ping google\"
bash: !\"ping: event not found

/root# !(ping google) 
bash: !: event not found

This calls the command, but it will call the last command, containing the string. I want only commands, beginning with "ping"

/root# !?google
ping google.com

Is it possible to do what I want?

share|improve this question
    
!?ping google (or even only !?ping g) would work. This would however also match echo ping google.com. –  Tim Aug 22 '13 at 14:07

2 Answers 2

up vote 1 down vote accepted

you can use backward search (control-r if the edit mode is set to emacs, which is the default) and it works with space.

share|improve this answer

I can think of a couple of ways:

  1. Use ?. The following is from man history:

    !?string[?]
          Refer to the most recent command preceding the
          current postition in the history list contain‐
          ing  string.  The trailing ? may be omitted if
          string is followed immediately by a newline.
    

    So, you should be able to do this:

     !?ping google?
    
  2. Use the reverse search feature. Hit CtrlR ans start typing ping google. Hit enter when the relevant command has been found.

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.