Search code examples
linuxmacossedmakefilebsd

sed on linux and MacOS in Makefile


I am writing a Makefile which is supposed to work on Linux and on MacOS systems. I make use of the sed program. It turns out that BSD sed and gnu sed differ in a small but crucial detail. I cite from the man pages:

BSD:
-i extension ...

GNU:
-i[SUFFIX], --in-place[=SUFFIX]

So on the Mac I have to call

sed -i '' -e 's/foo/bar/' file.tmp

where the empty string after -i is necesary, while on the Linux machine I call

sed -i 's/foo/bar/' file.tmp

Here is the question:

Is there a safe way to distinguish between the two OS's in the Makefile?

I can of course have the user call Makefile.mac or Makefile.linux, but I would rather avoid this.


Solution

  • The safe way is to not use the -i option of sed. This option is an unportable extension and is not part of POSIX.