Super User is a question and answer site for computer enthusiasts and power users. 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

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
  515  ping
  516  ping
  517  ping
  518  ping
  519  ping
  520  ping

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

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

/root# !ping  

A simple string with space after ! does not work

/root# !ping google
ping 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 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

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 – Tim Aug 22 '13 at 14:07
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:

          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


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.