18

This question already has an answer here:

This is a very weird behavior. Lately, whenever I am trying to find a process on console with ps aux | grep foo, it is returning a grep: command not found. Ok, that could be something that I changed and did wrong, but the problem is that, after a few tries, it suddenly knows what grep is:

MM:.dev$ ps aux | grep foo
-bash:  grep: command not found
MM:.dev$ ps aux | grep foo
-bash:  grep: command not found
MM:.dev$ ps aux | grep foo
-bash:  grep: command not found
MM:.dev$ ps aux | grep foo
MM            70953   0,0  0,0  2699108    744   ??  Ss   10:32AM   0:08.77 foo bar
MM             3756   0,0  0,0  2423356    204 s004  R+   10:12AM   0:00.00 grep foo

Any ideas why is this happening?

As suggested, this is what I get when a enter type grep after both, the one failing and the one working:

MM:.dev$ ps aux | grep foo
-bash:  grep: command not found
MM:.dev$ type grep
grep is /usr/bin/grep
MM:.dev$ ps aux | grep foo
MM             5694   0,0  0,0  2433796    676 s006  S+   10:47AM   0:00.00 grep foo
MM:.dev$ type grep
grep is /usr/bin/grep

marked as duplicate by Allan, Monomeeth, nohillside Apr 3 '17 at 5:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Strange. Do you retype the command every time or just scroll back in the command history? – nohillside Dec 16 '15 at 9:58
  • I was about to write that. When I scroll back it does not work. If I write it back, it doesn't work either. But if I scroll back and delete the grep and the pipe, it eventually works. O_O – makeMonday Dec 16 '15 at 10:05
36

Looking at your output, there seems to be an extra space before grep in the error message. I assume this is coming from a still pressed Opt/Alt key after typing the pipe symbol (so you basically are typing Opt-Space instead of just Space). Try ps aux |grep foo (without a space between the pipe and the g).

  • 2
    That's it :D Normally, I use emacs, and there when I accidentally type Opt+Space I can see something similar to _. So in the terminal, I was just missing that. Thanks :) – makeMonday Dec 16 '15 at 10:39
  • Oh my god.. happened so often to me and that was simply it. how silly! – Marc-Alexandre Bérubé Feb 2 '17 at 15:06
  • this has happened so many times to me, thanks so much for your answer, really appreciate it, i'll be sure to type |grep from now on !! I've been wondering about this one for years! – esaruoho Jul 9 '17 at 14:28
6

My answer may sound strange, but I had this effect with an old keyboard as well when I typed too fast. Just try typing it again, move with your cursor before the letter "g" of grep, delete the whitespace that could have been entered with a still pressed ALT/Option key, hit the space key again and fire up your commandline.

Another workaround is to lower the keyboard repeat rate in System Preferences, if you are not typing with ten fingers. :-)

  • That was it! @patrix suggested that too. Stupid of me for not realizing, but sometimes I just want to type too fast. Thanks. – makeMonday Dec 16 '15 at 10:44

Not the answer you're looking for? Browse other questions tagged .