Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What's the difference between os x sed and bsd sed?


Asked by Cameron O’Connell on Dec 11, 2021 FAQ



OS X currently comes with a FreeBSD sed from 2005. Most of the differences below also apply to other BSD sed versions. OS X's sed uses -E for ERE and GNU sed uses -r. -E is an alias for -r in GNU sed (added in 4.2, not documented until 4.3).
Furthermore,
OS X currently comes with a FreeBSD sed from 2005. Most of the differences below also apply to other BSD sed versions. OS X's sed uses -Efor ERE and GNU sed uses -r. -Eis an alias for -rin GNU sed (added in 4.2, not documented until 4.3). Newer versions of FreeBSD and NetBSD sed support both -Eand -r. OpenBSD sed only supports -E.
Indeed, BSD sedis fussier about the layout than GNU sed. There are a number of GNU extensions that are not part of BSD sed, and this is one. The semi-colon isn't the universal solution to problems, though it does solve many of them. You could also spread the commands out over multiple lines, or put them in multiple -eoptions, or various other 'tricks'.
Moreover,
The sed utility reads the specified files, or the standard input if no files are specified, modifying the input as specified by a list of commands. The input is then written to the standard output.
In respect to this,
Macs use the BSD version of utilities such as sed and date, which have their own idiosyncrasies. The shown command will replace all instances of needle with pin in the file haystack, and the original file will be preserved in haystack.bak.