Freebsd – How to make GNU grep the default in FreeBSD

defaultsfreebsdgreppath

I'm running FreeBSD 9.1-RELEASE. I've installed GNU grep with portmaster textproc/gnugrep.

However the "default" grep for users is still FreeBSD grep.

# /usr/local/bin/grep -V
/usr/local/bin/grep (GNU grep) 2.12

# grep -V
grep (GNU grep) 2.5.1-FreeBSD

I want to make GNU grep the default. I understand that the problem is with the order of directories specified in my PATH environment variable:

# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin

However, I fear to move the /usr/local/bin entry to the beginning of my PATH. Is it safe?

In Linux distros like Debian such tasks are usually accomplished via dpkg-divert and/or update-alternatives.

What is the best way to do what I want in FreeBSD and not break system upgrades and such?

Best Answer

Update: Note this answer is from 2013, it applies to FreeBSD 8.x and earlier. A BSD grep was added in revision 222273 and appeared in FreeBSD-9.0 (oddly that change is missing from the usually comprehensive release notes: Google search). A fully-featured GNU grep continues to be available in the ports collection.


FreeBSD grep is was GNU grep, albeit old and with a few patches applied:

# which grep 
/usr/bin/grep

# /usr/bin/grep -V
grep (GNU grep) 2.5.1-FreeBSD

Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

It has a small number of patches (most of which originate from Fedora Linux), if you have /usr/src/ installed those are detailed in /usr/src/gnu/usr.bin/grep/FREEBSD-upgrade.

If you need something specifically in the port version (2.12 vs 2.5.1) there are many bugfixes, speed improvements, and PCRE support (-P, not enabled in system version), it should be quite safe to reorder your PATH to put /usr/local/bin first, this is what I usually do. (It's good practise to use su - so that root's environment is set correctly, though on FreeBSD the default ~root/.cshrc sets the PATH explicitly.)

Otherwise check your shell man page and set an alias as required, but this is really only for interactive use, shell scripts or Makefiles won't observe it.

Related Question