Grep command! No sense here!

I finally went crazy today. I work everyday with Unix-like systems (Linux and FreeBSD), but my main system is OS X, another Unix.

The problem is, I can't count on grep command working on OS X. Let me explain what happens with an example:

Last login: Fri Feb 11 17:33:41 on ttys000
macbook:~ nicolunacba$ ls | grep <--- Okay, the command exists (1)
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
macbook:~ nicolunacba$ ls | grep dada <---- Why it doesn't exist now? (2)
-bash: grep: command not found
macbook:~ nicolunacba$ ls | grep <---- But it existed in (1), why not now?! (3)
-bash: grep: command not found

What's happening? I am sick of using VMs with Ubuntu when I should do the same in OS X. I work everyday with Bash scripts.

Thanks.

MacBook White 13'' - Late 2007 - 2 GB Ram, Mac OS X (10.6.4), iMac G3 400 Mhz (using Mac OS 9)

Posted on Feb 11, 2011 6:15 PM

Reply
7 replies

Feb 11, 2011 7:48 PM in response to nicolunacba

I finally went crazy today. I work everyday with Unix-like systems (Linux and FreeBSD), but my main system is OS X, another Unix.

The problem is, I can't count on grep command working on OS X. Let me explain what happens with an example:

Last login: Fri Feb 11 17:33:41 on ttys000
macbook:~ nicolunacba$ ls | grep <--- Okay, the command exists (1)
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
macbook:~ nicolunacba$ ls | grep dada <---- Why it doesn't exist now? (2)
-bash: grep: command not found
macbook:~ nicolunacba$ ls | grep <---- But it existed in (1), why not now?! (3)
-bash: grep: command not found

What's happening? I am sick of using VMs with Ubuntu when I should do the same in OS X. I work everyday with Bash scripts.</div>
It works for me all day, everyday. At work, I've been using a Mac as my workstation for 6 years, developing kernel software (in C) for Linux, Solaris, AIX, and Windows, with heavy duty bash shell and Perl scripting on the side (such as multi-thousand line scripts). I'm just trying to say that my Mac gets heavy duty use on the Unix side of the street.

You might want to see what grep you are using

type -a grep

And you could try

ls | type grep
ls | grep
ls | type grep
ls | grep
ls | type grep
ls | grep
ls | type grep

And for fun, what happens if you try

ls | (type grep; grep)
ls | (type grep; grep)
ls | (type grep; grep)

Also I guess we should ask if you are running the version of 'bash' that came with Mac OS X, or if you have installed your own bash?

type -a bash
echo $0
echo $BASH_VERSION
bash --version

By the way, Terminal, Unix, X11, and command line command, ssh, rsync, etc... questions are best asked in the Mac OS X Technologies > Unix Forum
< http://discussions.apple.com/forum.jspa?forumID=735 >

Feb 12, 2011 2:49 PM in response to BobHarris

@Keppy, I swear I am not repeating lines. It was a Copy and Past from the terminal (added few comments).

@BobHarris, I am a programmer too and I use a lot of scripts (bash, perl and tcl). I am using the bash version of Mac OS X.

GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
Copyright (C) 2007 Free Software Foundation, Inc.

and grep 2.5.1 in /usr/bin/grep

May I recompile and install bash? Or what? I need this command. Let me see something. I am in holidays here (I'm from Argentina and it's summer here). I've not programmed for 2 months and maybe I'm crazy now.

If I do this:

macbook:~ nicolunacba$ ls -la

I'll get this (I'll make it shorter, it's really large):

drwx------+ 14 nicolunacba staff 476 Feb 12 16:07 Desktop
drwxr-xr-x 16 nicolunacba staff 544 Dec 7 01:50 Development
drwx------@ 81 nicolunacba staff 2754 Feb 8 15:43 Documents
drwx------@ 737 nicolunacba staff 25058 Feb 12 15:27 Downloads
drwx------+ 50 nicolunacba staff 1700 Feb 1 21:36 Library

So, if I do:

macbook:~ nicolunacba$ ls -la | grep "Development"

I should get:

drwxr-xr-x 16 nicolunacba staff 544 Dec 7 01:50 Development

Am I right? That's the problem, it doesn't work! I think I am the first person in the world with this problem.

I'll reinstall OS X tomorrow, I need to work but I'm still scared of this. Maybe I'll save a Back up Image of the system to study it later

Feb 14, 2011 6:17 AM in response to nicolunacba

May I recompile and install bash?

While I do NOT think it is your version of 'bash', as this is not a problem for anyone else, you can most certantly provide your own version of bash. But I think you would be chasing the wrong solution.

To build your own bash, you first need to have XCode installed. It is an optional install on your Mac OS X installation DVD (of course since you are a programmer, you many have already done this). Then you can download the 'bash' sources, expand the .tar file, run the 'configure' script, run the 'make' command, then "sudo make install" should put a copy of bash in /usr/local/bin/bash.

You can also get a pre-built bash from MacPorts.org

And since I do not think it is your version of 'bash', I would also ask what your PATH looks like, as that affects what commands get exectued:

echo $PATH
type -a grep

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Grep command! No sense here!

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