-4

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

grep 'bash' *.sh

BUT

fgrep 'bash' *.sh

works like a champ.

which grep

and

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.

6
  • What are you trying to achieve? Dec 17, 2012 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. Dec 17, 2012 at 21:14
  • I can't replicate your problem. Dec 17, 2012 at 21:17
  • Could it be you have an alias for grep? Does $(which grep) 'bash' *.sh work?
    – ott--
    Dec 17, 2012 at 21:20
  • Ott, thanks, that says syntax error near unexpected token 'bash' Dec 17, 2012 at 21:30

3 Answers 3

1

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: https://unix.stackexchange.com/questions/17949/what-is-the-difference-between-grep-egrep-and-fgrep

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.

0

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.

1
  • Yes, which is what is even more perplexing. Because fgrep pattern filename produces a result and grep -F pattern filename produces usage. Dec 17, 2012 at 21:24
0

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.

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