Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
1  
Umm... 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 Sep 18 '12 at 13:49
1  
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 Sep 18 '12 at 19:22
add comment

2 Answers

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.

share|improve this answer
    
There's always Boot Camp. :) –  kojiro Sep 18 '12 at 19:22
5  
For 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 Mar 30 '13 at 1:45
2  
brew install gnu-sed --default-names will install it as sed –  ivotron Jun 27 '13 at 0:47
    
Instead 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 Apr 11 at 10:08
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

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.