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.
cat). It's not a good idea to replace the MacOS coreutils with the GNU coreutils.
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.
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.
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_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.