I am buying an iMac. I've been working on Linux for a long time and I do a bunch of bash scripting. I was wondering if all the bash commands and features are available on Mac OS ? Anyone has any experience with Mac bash scripting ? I appreciate your input.

Bash itself will be mostly the same. There will be a few small differences but none of these will affect the portability of your scripts.

The major problem is that the MacOS coreutils are FreeBSD-based while the utilities you are used to are most likely from the GNU project. The FreeBSD coreutils are not always compatible with the GNU coreutils. There are performance and behavioral differences between the GNU and FreeBSD versions of sed, grep, ps, and other utilities.

You can install the GNU coreutils but they have g- prefixes (e.g. gcat for cat). It's not a good idea to replace the MacOS coreutils with the GNU coreutils.

  • 5
    It should be amplified that using the standard FreeBSD-based core utilities can affect the portability of your scripts (e.g., ps has different flags, as does grep). I've banged my head against the wall a couple of times trying to figure out why my Ubuntu script wasn't producing the same output on my Mac. – Chris Gregg Jul 9 '13 at 6:50
  • 1
    If you want to write your scripts to be portable, www2.opengroup.org/ogsys/jsp/publications/mainPage.jsp is a great place to get the strictly-standard specifications for POSIX and XCU utilities and commands. – kojiro Jul 9 '13 at 9:21
  • @ChrisGregg: I agree. I've added a bit to my answer to emphasis that there are incompatibilities between the two tool sets. – Smith John Jul 9 '13 at 11:07

If it matters, bash on Mac OS X Mountain Lion is still version 3.2. Most linux distributions these days are shipping with 4.x so you'll be missing version 4 features. At current, version 4.2.45 is available from Homebrew as a workaround. I don't know what version is in Mavericks, I'm not sure anybody who knows is allowed to say yet.

As mentioned by others, bash is the same bash (other than the already noted versions) but Mac OS X uses a (mostly) BSD userland. Many of the standard system utilities have slightly different options or behave slightly differently. They're unfortunately too numerous to list here.

  • 1
    4 years later bash on High Sierra is still using: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17) Copyright (C) 2007 Free Software Foundation, Inc. – Nate Lockwood May 17 at 14:57

Install homebrew (a package manager) then install the gnu versions of some basic utilities:

$ brew install coreutils
$ brew install gnu-sed

After each installs, it will show steps that you can follow so that you can simply call date instead of gdate for example, so that you can still use your Linux bash scripts without any modification.

Congratulations, you've just solved 99% of your cross-platform scripting issues.

  • 3
    On the other hand, if your scripts are actually meant to be portable, you'll want to fix the scripts, not the symptoms. – tripleee Jul 9 '13 at 11:12

One thing that can be frustrating coming from Linux to a Mac is that there is no /proc pseudo-file system. This has broken a number of my Linux scripts, and it can be tricky to find suitable ways to get the same information you find in proc into your Mac bash script.

I'm a 'mainly linux, occasional mac' user and I find that:

bash, ~/.bashrc and ~/.bash_aliases mostly work the same.

One issue I have found was one of the command to change the color prompt didn't work on the Mac. From that and other experiences I've learned to keep a terminal window open and logged in when setting up my .bashrc file. That was I can test 'new window' and find any errors in the still logged in one.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.