Replace OS X's shell commands with the linux versions?

32

7

The commands available by default in the Terminal in OS X, don't seem to behave like their linux versions. How can I replace all of them with the actual GNU linux commands?

For example sed -i requires an annoyingly additional "" argument. Also, it only works with text files. This is useless.

TheOne

Posted 2012-09-18T13:12:56.667

Reputation: 1 265

1Umm... sed is a "text editor", it can also modify binary files. What else would you want it to do? Please explain what exactly you need. I cannot understand your problem, could you give some more examples? – terdon – 2012-09-18T13:49:32.050

3

Actually, sed is a stream editor. Not to be pedantic, but it's actually not meant to modify files, unless those files are streams. That's precisely why the -i flag is non-standard. ed is for editing files.

– kojiro – 2012-09-18T19:22:05.217

Answers

43

In the general case, you can't (or shouldn't) replace the default commands at all. The reason is that many system administration scripts and third-party packages probably rely on these commands to behave the way they do out of the box on OS X.

So if you just wipe out the system commands and replace them with GNU equivalents that have incompatible behavior or command line arguments, it will probably break something. Especially if you use some software that was "ported" to Mac OS X after being originally designed to run on Linux or BSD, as these types of programs are more likely to rely on shell scripts and system commands as opposed to calling OS X APIs.

What you can do is install an environment that installs the GNU utilities in another directory without overwriting the defaults, and then adjust your PATH environment variable so that it gives priority to commands found within the GNU directory before it even searches the system directories. You can wire this up so that it only sets your PATH that way if you are starting an interactive shell; you can google how to do this with bash or ask another question on SU (or search for it, since it's probably been asked before) if you want to do that.

An example of such an environment is Homebrew which for example has GNU sed among other things. Once you've installed Homebrew, you can type

brew install coreutils

and install the GNU Coreutils. These will provide you with sed, date, printf, wc and many other tools that ship with GNU/Linux, but not OS X. However, so as not to "override" default OS X binaries, they will be prefixed with g by default. So, after installing the Coreutils, if you want to use GNU sed, type

gsed

If this is too much of a hassle to type every time, you can add a "gnubin" directory to your PATH and just call GNU sed with sed. You will need to add the following to your ~/.bash_profile:

PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"

Of course, if you need a Linux environment from soup to nuts (kernel, X11, syscall compatibility, etc) you'll have to run Linux in a virtual machine, such as VirtualBox. This is a safe bet if you need to run software or scripts that are designed to run on Linux.

Homebrew will only afford you compatibility for certain classes of programs that do not require Linux-specific behavior. For example inotify is only available on Linux. drm (the Direct Rendering Manager) is only available on Linux. There are some other rather low-level system calls that are only available on Linux, and for which no equivalent exists on OS X, so porting certain programs from Linux to OS X can be impractical or impossible without significant code changes.

allquixotic

Posted 2012-09-18T13:12:56.667

Reputation: 32 256

There's always Boot Camp. :)

– kojiro – 2012-09-18T19:22:47.600

13For whatever reason brew install coreutils doesn't include sed. You can do brew install gnu-sed however this installs gsed and not sed, even if you update your $PATH. I created a symlink to hide Mac's sed: ln -s $(which gsed) $(brew --prefix coreutils)/libexec/gnubin/sed however you still have to do man gsed to see the right manpage. – dimo414 – 2013-03-30T01:45:05.043

6brew install gnu-sed --default-names will install it as sed – ivotron – 2013-06-27T00:47:51.957

1Instead of --default-names, you can install with the g-prefixed commands and then install oh-my-zsh (highly recommended) and enable the plugin gnu-utils. – bibstha – 2014-04-11T10:08:19.620

3

You could use a Gentoo Prefix which supports OS X, you can do this by bootstrapping it and then adding the relevant paths in the prefix directory to your PATH. It might be that this already does that for you. After you have done that you can use standard Gentoo commands for installing packages.

emerge coreutils will get you the standard GNU utils, for instance.

Please note that Gentoo compiles by default, you might want to set up a binary host instead. This is just using one of the URLs on the second half of that article, and placing it in PORTAGE_BINHOST="... your url here ..." in ./etc/make.conf in your prefix.

Tamara Wijsman

Posted 2012-09-18T13:12:56.667

Reputation: 54 163

1

As a follow up to @allquixotic's post, here are the official instructions per brew install coreutils

All commands have been installed with the prefix 'g'.

If you really need to use these commands with their normal name can add a "gnubin" directory to your PATH from your bashrc like

PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"

Additionally, you can access their man pages with normal names the "gnuman" directory to your MANPATH from your bashrc as well

MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"

user72923

Posted 2012-09-18T13:12:56.667

Reputation: 867

0

With respect to sed you may download a precompiled binary from the Rudix project. For further Rudix packages see here and here.

kerny

Posted 2012-09-18T13:12:56.667

Reputation: 1