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

230

Solution 1

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.

Solution 2

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.

Solution 3

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"
Share:
230

Related videos on Youtube

AndréB
Author by

AndréB

Updated on September 18, 2022

Comments

  • AndréB
    AndréB almost 2 years

    I'm working on an app which will make heavy use Vaadin 8's Grid. I need to allow the user to scroll through/filter/sort millions of rows quickly.

    The grid I have built here as a prototype works well using lazy loading, as long as the amount of data is somewhat small. Above a certain number of records, a well-known bug shows up:

    https://github.com/vaadin/framework/issues/6290

    The behaviour can be tested on Vaadin's very own demo site:

    https://demo.vaadin.com/sampler/#ui/grids-and-trees/grid/large-dataset

    Open the link in Internet Explorer or Edge and see for yourself. I used IE 11.98.16299.0:

    Vaadin 8 Grid in IE 11

    Short: IE does not show more than 40k entries, Firefox fails at 200k.

    First entry in the issue log 2015, last entry October 2017. The vaadin guys say that they won't fix it in 8.x, I should wait for 10 or pay for a consultant.

    Does anyone have a workaround in place for this?

    Sidenote:

    There's a reason that I didn't provide any own code snippet. The thing we're building here is based on existing code my company wrote using the ULC framework. There's no static table, no static field list, everything is dynamic and hence pretty difficult to paste here - because snippets alone don't make any sense. Think of the project as "generic database browser".

    • Admin
      Admin almost 12 years
      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?
    • Admin
      Admin almost 12 years
      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
    kojiro almost 12 years
    There's always Boot Camp. :)
  • dimo414
    dimo414 over 11 years
    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.
  • ivotron
    ivotron about 11 years
    brew install gnu-sed --default-names will install it as sed
  • Bibek Shrestha
    Bibek Shrestha about 10 years
    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.