Server Fault is a question and answer site for system and network administrators. 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

Well, I am perplexed. I am working with an Ubuntu server and I type in

grep 'bash' *.sh


fgrep 'bash' *.sh

works like a champ.

which grep


which fgrep

both point to their respective executables in /bin. I am perplexed as to what I am doing wrong.

EXAMPLE output:

$ grep -F 'grounding' repl.clj
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c

$ fgrep 'grounding' repl.clj 
(p/concepts-for-grounding-term imp1  "PERSON" "summary")

See? grep is failing but fgrep is working fine. That is why I am perplexed.

share|improve this question

closed as off topic by Tom O'Connor, Brent Pabst, Zoredache, Michael Hampton, Scott Pack Dec 18 '12 at 0:33

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

What are you trying to achieve? – Tom O'Connor Dec 17 '12 at 21:12
I want grep to work, I am just giving basic examples. It is only return help (usage) as if I did something wrong but no amount of rework seems to make a difference. – Pete Mancini Dec 17 '12 at 21:14
I can't replicate your problem. – Tom O'Connor Dec 17 '12 at 21:17
Could it be you have an alias for grep? Does $(which grep) 'bash' *.sh work? – ott-- Dec 17 '12 at 21:20
Ott, thanks, that says syntax error near unexpected token 'bash' – Pete Mancini Dec 17 '12 at 21:30

grep, egrep, and fgrep have different behaviors. You don't show in your question what you're getting back from each of them, so it's hard to say more. The differences are discussed here:

The fact that they are the same executable on the filesystem isn't important, since it is likely that they change their behavior based on their own name. This is a minor optimization, to install and hardlink a single version of an executable to different names.

share|improve this answer

I found this in the grep man page:

   In  addition,  three  variant  programs  egrep,  fgrep  and  rgrep  are
   available.   egrep  is  the  same  as  grep -E.   fgrep  is the same as
   grep -F.  rgrep is the same as grep -r.  Direct  invocation  as  either
   egrep  or  fgrep  is  deprecated,  but  is provided to allow historical
   applications that rely on them to run unmodified.

fgrep is just a shortcut. So is egrep.

share|improve this answer
Yes, which is what is even more perplexing. Because fgrep pattern filename produces a result and grep -F pattern filename produces usage. – Pete Mancini Dec 17 '12 at 21:24

Ott - you got me thinking and I looked at my .bashrc file and sure enough it had corruption in it on the line that aliased grep. I fixed the line and now it works.

Learn from this lesson. Try to never ever alias or obfuscate the default commands like ls grep-- it will only come and bite you later on.

Instead of aliasing something like this:

alias grep='grep --color=auto --whoopssometypo'

Give your alias an original name:

alias grepc='grep --color=auto --whoopssomettypo'

This way, you aren't breaking default commands and you can quickly spot typos through some quick tests. If grep works, and grepc does not work, you can quickly tell that your problem is with grepc not grep.

If you had made your modification to an environment file which was shared by multiple admins, you could have caused substantial problems at your workplace.

I sometimes make one exception to this rule. Many Linux distros will alias a handful of destructive commands. Here is an example from a RHEL5 system:

# alias
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'

The alias rm -i has saved me many times. However, relying on this alias is a bad habit, because sooner or later you'll be on a system where rm is not rm -i and you'll destroy files accidentally.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.