We are trying to find a way to craft a GNUmakefile that works on nearly all Unix compatibles. We were using egrep but it failed on MinGW. We switched to grep -E because it is Posix, but testing shows it is failing on Solaris 11.

A typical usage in the makefile is:

GREP ?= grep

IS_X86 := $(shell uname -m | $(GREP) -v "x86_64" | $(GREP) -i -c -E "i.86|x86|i86")
IS_X64 := $(shell uname -m | $(GREP) -i -c -E "(_64|d64)")

The result is:

grep: illegal option -- E
Usage: grep [-c|-l|-q] -bhinsvw pattern file . . .
grep: illegal option -- E
Usage: grep [-c|-l|-q] -bhinsvw pattern file . . .

I don't see where -E and -F are optional in Posix (perhaps I missed it). I found Difference between Solaris and POSIX on Stack Overflow, but it states, "Solaris has a full set of POSIX interfaces ...".

Why is Solaris' grep failing to consume -E? Is there a way to engage regular expression support on Solaris besides switching to egrep?

I just setup a fresh test account on the Solaris 11.3 machine to ensure I did not taint the environment.

The account was added with:

# useradd -d /export/home/test -m -s /bin/ksh -c "Testing Accoung" test

80 blocks

Inspecting PATH after logging in:

test@solaris:~$ echo $PATH
  • you probably want to use the ones in /usr/xpg4/bin -- see unix.stackexchange.com/questions/198905/… – Jeff Schaller Aug 29 '17 at 1:39
  • 1
    Since you can assume a shell, you should be able to use a case statement rather than using grep. What exactly are you looking for? I can code up something once I am not on a phone. – icarus Aug 29 '17 at 3:53

From the Oracle Solaris docs (XPG)


If the behavior required by POSIX.2, POSIX.2a, XPG4, SUS, or SUSv2 conflicts with historical Solaris utility behavior, the original Solaris version of the utility is unchanged; a new version that is standard-conforming has been provided in /usr/xpg4/bin. For applications wishing to take advantage of POSIX.2, POSIX.2a, XPG4, SUS, or SUSv2 features, the PATH (sh or ksh) or path (csh ) environment variables should be set with /usr/xpg4/bin preceding any other directories in which utilities specified by those specifications are found, such as /bin, /usr/bin, /usr/ucb, and /usr/ccs/bin.

If you must use extended regular expressions, set /usr/xpg4/bin early in your PATH and use grep -E.

  • Thanks. That's very unusual behavior from Solaris. They usually conform to some standard, even if its painful. I'm surprised to see a utility with arbitrary behavior at the head of the list. By the way, /usr/ucb is deprecated and should not be used. – user56041 Aug 29 '17 at 10:18
  • That's how Solaris has been for at least two decades. – muru Aug 29 '17 at 10:20
  • Thanks @muru. I just tested on FreeBSD. FreeBSD grep consumes -E. (Did you intend to link to FreeBSD docs?) – user56041 Aug 29 '17 at 10:34
  • @jww I linked to SunOS 5 docs, which are available on FreeBSD's manpage repository – muru Aug 29 '17 at 10:37
  • Thanks again @muru. So it sounds like FreeBSD 10 and 11 are not sufficient for testing; and I need to add a new machine to my test environment. Would you happen to know which version of FreeBSD has the same behavior as Solaris? Or, do you know if FreeBSD has the /usr/xpg4/bin path? – user56041 Aug 29 '17 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy