Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

bash: command not found after piping

Hi,


I'm trying something quite commonly found in an Unix environment, namely piping the output of one command into the input of a subsequent one, eg.


bash$ ps ax | grep -i bash


But unfortunately I'm receiving--and now comes the most curious part--most of the times (not always!!) an error message


bash: grep: command not found


When calling grep directly


bash$ grep


the resulting output is


Usage: grep [OPTION]... PATTERN [FILE]...

Try `grep --help' for more information.


"which"-ing the commands


bash$ which bash ps grep


results in


/bin/bash

/bin/ps

/usr/bin/grep



Cheers,

MMHein

Mac mini, Mac OS X (10.7)

Posted on Jan 29, 2012 1:15 AM

Reply
32 replies

Aug 17, 2012 9:52 AM in response to MMHein

I believe I can shed some light on this issue. Since I bought my first MacBook Pro in 2009 I've occasionally seen this problem, but I've not let it me bother me too much as I could work around it. Even when buying a new MBP earlier this year I got the same problem. Now I do a lot of programming in Java using Eclipse. And the few times I use the OR operator (pipe symbol) the space after it will be underlined, indicating an illegal character. So what I do is to step back. Delete the character and type space. I figured this was just some quirk in Eclipse and did not connect the two until today.


So I tried the same when in the shell when typing a command with several pipes - and the command worked! I took a look at the command in a hex editor and strangely found "7CCA" instead of "7C20" ("|" followed by a space). Playing around with the hex editor I also saw that holding Alt+7+Space together always produced "CA".


Now I have Norwegian keyboards so I suspect that there is some software issue which is also present when using German keyboards.

Oct 19, 2012 3:50 AM in response to MMHein

The issue is that you're accidentally pressing the Option modifier key and typing Option-Space, which enters a non-breaking space, which isn't recognized by the shell. You can configure bash to treat non-breaking spaces as regular spaces by putting the following in ~/.inputrc:


# Map (UTF-8) non-breaking space to regular space, in case the user

# accidentally types Option-Space when they meant Space.

"\xC2\xA0": " "

Mar 30, 2014 4:42 PM in response to MMHein

YayBjorn,


The other scenario where I have seen grep return the error "No Such file or Directory" is when you have a misconfigured environment variable:


For example if we add the following line to .bash_profile


set GREP_OPTIONS=="--color=always"


and then run [ source .bash_profile; /bin/ps ax | /usr/bin/grep -i grep ] we get the error...


$ source .bash_profile; /bin/ps ax | /usr/bin/grep -i grep

grep: grep: No such file or directory


if we edit the .bash_profile and remove the second '=' sign


set GREP_OPTIONS="--color=always"


and then rerun the command [ source .bash_profile; /bin/ps ax | /usr/bin/grep -i grep ] we get no errors


$ source .bash_profile; /bin/ps ax | /usr/bin/grep -i grep

78631 s001 S+ 0:00.00 /usr/bin/grep -i grep

bash: command not found after piping

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.