There's a good chance you're running into some character coding issue. The file you're trying to grep could be in a different character encoding than your system's default encoding. Unixy systems typically default to UTF-8 these days, which is compatible with 7-bit ASCII, but not with any of the 8-bit ASCII extensions. Common 8-bit encodings in the US are ISO 8859-1 and Windows CP-1252. There are dozens more used in the rest of the world.
grep
assumes all input is in your default system encoding. To grep
a file in a different encoding, use iconv
to convert it:
$ iconv -f iso8859-1 -t utf8 myfile.txt | grep something
I realize this is highly inconvenient for your recursive example, but the broader lesson is that if that fixes the problem, you should convert all the text files in that directory tree so they're compatible with your system character encoding. If you need Windows text editor compatibility, don't worry, most Windows text editors that focus on code editing cope with UTF-8, even though Windows uses UTF-16 natively these days.
Another possibility is that your file uses curly quotes. The quotes you type on your keyboard are straight quotes -- ASCII 39 -- but some word processors and text editors replace them with curly quotes, or U+2019 in this example.
I like to use this command for poking through a file to investigate character coding issues:
$ od -t x1 < myfile.txt | less
There are various "hexdump" programs available, but they often do unhelpful things like display the data as 16-bit words in little-endian format. Because od
doesn't also have a printable text display column like any decent hexdump program, though, it works best for short files. I often cut down the example to something easy to test first.
I probably would define an alias with my options, e.g.:
alias grep="grep --ignore-case --color"
as this would only affect interactive programs and not scripts. You could then just run \grep
or /bin/grep
to run it without any options.
If you want to keep using GREP_OPTIONS
you can just unset it for your commandline, e.g.
GREP_OPTIONS= grep ....
Best Answer
If
grep o
produces color output, then eithergrep
is an alias togrep --color=auto
orgrep --color=always
(or possibly more options), orGREP_OPTIONS
is set to a value that contains--color=auto
or--color=always
. Since$GREP_OPTIONS
is empty, it must be the alias.Since
grep o | less -R
doesn't show colors, the alias must be togrep --color=auto
(a sensible choice). With the alias, thegrep
command always receives the--color
option on the command line, and this takes precedence over the environment variable.If you want to use the environment variable, remove the alias definition from your
~/.bashrc
, or for one session rununalias grep
. You can replacealias grep='grep --color=auto'
byexport GREP_OPTIONS='--color=auto'
: they have essentially the same meaning, except that:GREP_OPTIONS
to a different value only overrides the latter;grep
from an interactive shell, whereas settingGREP_OPTIONS
also applies whengrep
is run from scripts and other applications.Never put
--color=always
or most other options inGREP_OPTIONS
: it would break many programs that parse the output ofgrep
.--color=auto
is about the only safe option to put inGREP_OPTIONS
. For anything else, use the alias. Future versions of GNU grep will drop support for the option for this reason.Note that the alias definition goes into
~/.bashrc
(it's a shell setting), whereas the environment variable definition goes into~/.profile
(it's a session setting). See Is there a ".bashrc" equivalent file read by all shells?If you want to run the unaliased command just once, run
\grep
instead ofgrep
(quoting any part of the name bypasses the alias lookup).