Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

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

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?

share|improve this question
up vote 0 down vote accepted

If you don't want to change your ${PATH}, alternatively you can just link to the grep you like from an early entry of the ${PATH} value.

for example /bin is the second entry of your ${PATH} and its probably in all users's ${PATH} values.

so you could do this as root:

cd /bin
ln -s /usr/local/bin/grep
share|improve this answer
This is a very bad idea. Package managers rely on the veracity of their databases to maintain state -- that trust should not be circumvented. – Chris Down Feb 21 '13 at 10:18
@ChrisDown as far as I can tell, this solution doesn't break package manager databases. No port will install binaries to /bin ... – vadipp Feb 21 '13 at 10:21
ok, you are right about that... alternatively. you could add something in your ~/.bashrc like export PATH="~/.bin:${PATH}". and then you link from there, like mkdir ~/.bin; cd ~/.bin; ln -s /usr/local/bin/grep. That way only your user is affected. – mauro.stettler Feb 21 '13 at 10:21
@mauro that's a clean and simple solution, thanks a lot. – vadipp Feb 21 '13 at 10:22
@vadipp I can't say it will cause problems with package management, I would not do this on FreeBSD because 1: grep is used during startup (it's not invoked before filesystems are mounted, but it's a concern) 2: it might complicate freebsd-update binary updates, similarly freebsd-update IDS if used 3: risk of breaking things by replacing/overriding system binaries with ones that have different switches (e.g. GNU sed has no -E, rc.jail would break if you replaced sed) 4: the alternatives (alias or PATH) are trivial. – mr.spuratic Feb 21 '13 at 12:32

FreeBSD grep is GNU grep, with a few patches applied:

# which 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

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, this is what I usually do. (It's good practise to su - so that root's environment is correct, though on FreeBSD the default ~root/.cshrc set 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 and Makefiles won't observe it.

share|improve this answer
Yes, I need the -P switch. I'll try changing the order of the PATH. Thanks! – vadipp Feb 21 '13 at 15:35

Just create an alias, in bash add to your ~/.bash_profile:

alias grep /usr/local/bin/grep

(I believe other shells can do something similar.) That way you can cherry pick individual executables. Even call your alias gnugrep if you don't want to loose the system one.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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