Piping in bash

Discussion in 'macOS' started by Lingonsylt, Dec 5, 2012.

  1. Lingonsylt macrumors newbie

    Dec 5, 2012

    I'm sort of ashamed that I have to ask this, as a long-time Linux user, but: why the error message below? (Mountain Lion)

    'grep' exists and is in $PATH:

    js-MacBook-Pro:~ j$ grep
    Usage: grep [OPTION]... PATTERN [FILE]...
    Try 'grep --help' for more information.

    But I can't pipe through it:

    js-MacBook-Pro:~ j$ find . | grep -i jpg
    -bash: grep: command not found

    Help appreciated!
  2. rhoydotp macrumors 6502


    Sep 28, 2006
    you should run

    find . -type f |grep -i jpg

    EDIT: actually, your command worked for me as well. strange :(

    mlmac:~ rhoy$ find . | grep -i jpg | head -2
    ./Documents/Misc Docs/GBC/concert/Calvary.jpg
    ./Documents/Misc Docs/GBC/concert/cross.jpg
    mlmac:~ rhoy$
  3. balamw Moderator


    Staff Member

    Aug 16, 2005
    New England

    find . | grep -i jpg
    works fine for me?!?

  4. Lingonsylt thread starter macrumors newbie

    Dec 5, 2012
    Wow, this is so frustrating.. It just seems that sometimes it works, sometimes it doesn't. I haven't been able to figure out the pattern.

    However, it seems that I can always manifest the problem by doing this:

    1. If Terminal is running, quit it.
    2. Open a new Terminal.
    3. TYPE (don't use up arrow go retrieve an old command, and don't paste anything) " ls -l . | grep jpg". Again, it's important that you TYPE, NOT PASTE.
    4. If you do happen to reproduce the problem by doing (3), if you the press up and edit away the space between the pipe and word "grep", then it *will work*.

    It'd be very interesting to know if anyone can reproduce this. Myself.. I'm stumped. ;)
  5. Lingonsylt thread starter macrumors newbie

    Dec 5, 2012
    It does seem that, as suggested by rhoydotp, that it always works if I don't put a space between the | and grep (or less, or whatever command I want).

    I've been using "<space><pipe>" for 20 years, and it's always worked for me.. And OS X seems to be using bash. I think I know how to work around this problem, but it'd be interesting to learn its cause.
  6. therealseebs macrumors 65816


    Apr 14, 2010
    My guess: You have typed an invisible character, so you have a character that you can't see, but which is not a space and is part of the name, so it's really "Xgrep: command not found" where X is invisible to you.
  7. rhoydotp macrumors 6502


    Sep 28, 2006
    real strange ... this is not an answer but more of a suggestion of what I would have done instead of this pipe

    find . -name \*jpg -print

    btw, i didn't really suggest anything with the spaces. spaces before/after pipe shouldn't matter, at least in my experience.
  8. Lingonsylt thread starter macrumors newbie

    Dec 5, 2012
    therealseebs: I see what you mean with the pasting thing. However, I often see the problem even when not pasting, just typing.

    Thanks for the input, guys. I'll work around the problem for now.

Share This Page