DEV Community

Cover image for Differences Between MacOS and Linux Scripting
Jonathan Boudreau
Jonathan Boudreau

Posted on • Edited on

Differences Between MacOS and Linux Scripting

Photo by Sharon McCutcheon on Unsplash

I often hear people saying MacOS and Linux are similar. I would like to propose that they are in fact very different.

But why does this matter? Well, for one, I write a lot of code for CI (Continuous Integration). The differences are big enough to make it challenging to re-use more complex scripts locally and in CI.

The Shell

MacOS recently changed its default shell to zsh. Before this, it was using a decade old release of bash.

Now, nothing is stopping you from installing the latest version of bash. If you're writing scripts for your team it is unlikely you will have much control over that however.

Package Manager

This is probably the most significant difference with MacOS and Linux when it comes to scripting automation. Most people on Mac rely on Homebrew, while most servers on Linux use apt. Not only is it different to install packages, but so is adding new software sources.

Docker

Since Docker for Mac runs in a virtual machine (using linuxkit), there are significant differences with running containers on it.

Volumes (bind mounts) have poorer performance. This may limit how many things you can throw at compose if your project is a large codebase.

File permissions map differently from the container to the host. Linux may use subordinate uids and gids, whilst MacOS maps all users to one uid/gid on the host.

find

MacOS uses a different implementation of find. Linux has a couple of extra options, and the search path is optional.

netstat

Similarly to find, netstat on Linux is a different implementation altogether. For certain tasks you will need to use lsof when on MacOS. The output for lsof is also different, so you will need to implement different parsing code for each operating system.

ifconfig

ifconfig is deprecated on Linux. It is recommended to write new scripts with ip instead. Since MacOS doesn't have ip, you're stuck with writing an implementation for each operating system.

xargs

xargs, which is used for piping arguments to another command, works slightly differently on MacOS. If the previous command has no output, xargs on MacOS won't run the command.

init

MacOS uses launchd, which accepts an xml configuration for the definition of new services. Most modern Linux distributions rely on systemd, which has a INI-like configuration format.

In Closing

Did you run into other discrepancies between the two operating systems? What are some of the best practices you follow to write portable scripts?

Top comments (13)

Collapse
 
jessekphillips profile image
Jesse Phillips

I would preposition you that this in fact shows that they are in fact very similar, but not the same.

Try and do something similar for Windows and your list would not only be longer but have completely different command structures, plus the choice of cmd vs powershell.

Collapse
 
aghost7 profile image
Jonathan Boudreau

Where I currently work this isn't as much the case. For the most part Mac and Windows just run a virtual machine with Linux on it. So you could say that Mac is more similar to Windows than Linux.

Collapse
 
jessekphillips profile image
Jesse Phillips

Mac and Windows are more similar because they are both avoided so that we can use Linux?

Could we not stay with similar comparisons that prompted you article, rather than completely shifting the means of comparison.

Thread Thread
 
aghost7 profile image
Jonathan Boudreau

I was only replying to your own comparison, it is true that Windows is not relevant to the article.

Thread Thread
 
jessekphillips profile image
Jesse Phillips

I was responding to your opening statement.

I would like to propose that they are in fact very different.

The article did a great job highlighting the differences between two similar things. It however was not good at showing similar things were very different.

This really isn't that important of a distinction, but it can reinforce a thought against those statements, like this comment.

"I have met so many people who think the using MacOS is like using a Linux distro, since it's based on Unix, which is just simply not true."

I don't utilize Mac myself, so your article was interesting, but I could see the exact same article to make the claim that using Mac is like using Linux.

What would change is the language around the differences. For example

Mac OS has made zsh the default shell. This is actually a better language for scripting and you could with your IT and team to have it installed for every provisioned server allowing for easy sharing of scripts.

This creates more of a "we can have the same things" message over a message that they are hopelessly different.

Now I have tried to have It install a text editor rather than relying on notepad as the standard, that was very adversely opposed. But that is a different challenge from comparing the two operating systems.

Again not really an important point for your article, just feel based on how you responded it might be an important point for you.

Thread Thread
 
aghost7 profile image
Jonathan Boudreau • Edited

Mac OS has made zsh the default shell. This is actually a better language for scripting and you could with your IT and team to have it installed for every provisioned server allowing for easy sharing of scripts.

Adding zsh to everything is not going to be practical for most medium-sized and up organizations imo. There's a lot more than just the servers running the application. You have the CI server, agents, container images, supporting infra for logging, etc. Last company I worked at had over a thousand VMs.

This creates more of a "we can have the same things" message over a message that they are hopelessly different.

But you can't have the same things, as you point out it doesn't work out that way in practice. If you're entertaining installing additional software to normalize things, why not just use a VM from Windows/MacOs?

But that is a different challenge from comparing the two operating systems.

That will only shift the effort into IT maintaining all these packages. The differences will still be there, its just a different department that is responsible for it.

Thread Thread
 
jessekphillips profile image
Jesse Phillips

IT tends to have OS snapshots and a selection of standard software and configuration every machine goes through. Yes this shifts the responsibility, to where it belongs.

If you're just running Linux VMs to have standards, why are you bothering with Mac or Windows?

I wasn't trying to claim you could have all the same things, the example was to put a spin of similarity on a difference. When you start talking about practical maintenance of thousands of systems, you've moved the discussion away from similarity and differences to a different problem space.

I am quite happy to discuss those challenges and what might be done about them, I didn't think that was what the article was about.

Thread Thread
 
aghost7 profile image
Jonathan Boudreau

If you're just running Linux VMs to have standards, why are you bothering with Mac or Windows?

Because we have developers running Windows and Mac on their laptops.

I wasn't trying to claim you could have all the same things, the example was to put a spin of similarity on a difference.

I'm not sure I get your point but you could also say Windows and Linux have a lot of similarities, or MacOS and Windows.

I am quite happy to discuss those challenges and what might be done about them, I didn't think that was what the article was about.

Its not, the article is about differences between Mac and Linux.

Collapse
 
yaythomas profile image
yaythomas

macOS runs BSD grep by default, on Linux it's usually GNU grep. There's some crucial gotchas on differences on lazy eval on regexes. . .

Although in theory if you pop a sh shebang at the top of your script you should be good to go for x-platform. . . it's pretty easy to run into this sort of portability issue where grep or sed runs subtly differently on the different platforms, and it's a devil to debug.

If you add to the mix that your script might be running on a CI host, or inside a Docker (poss. Alpine, which compiles with musl-libc rather than glib), or on a techie's MacBook. . . portability joy it is not.

I got annoyed enough with this a few years ago (all those "works on my machine" little weird subtle errors that are so tricky to troubleshoot on remote/hosted services where the failing bit is at the end of a longer automation sequence) that I ended up writing an automation tool to allow me to work cross-platform more easily. It's here, just in case it helps anyone. . . github.com/pypyr/pypyr

Collapse
 
csaltos profile image
Carlos Saltos • Edited

sed it's also slightly different ... and any other command and tool that is more BSD style than GNU style.

So to be clear, macOS and Linux are both Unix, thus they are similar because of that ... but macOS is BSD based and Linux is GNU based, thus different.

If you look for the commands behaviour for BSD and their counterparts for GNU it will help a lot.

FUN FACT: Linux is just the kernel, the operating system is formally GNU/Linux but no one does that, or we should ? ... maybe "macBSD" and "linuxGNU" are better names ? ... no !!

Collapse
 
mjgs profile image
Mark Smith

Nice article - yeah they are very similar in a lot of ways but every now and then I’ve run into commands that use slightly different flags, so I’ve ended up having to add conditionals that check the OS type and use the relevant flags.

I’m really not into zsh, I tried it for a while a year ago and really didn’t like it at all. Switched back to bash and was much happier. I guess it doesn’t make that much difference that the default shell has changed in OSX, it’s easy enough to set up bash for personal use, but it does make me wonder if the platform is long term the best developer platform (for me at least).

Collapse
 
aurelievache profile image
Aurélie Vache

Thanks for your article
date command is also not the same in macos and linux ^^

In order to install Unix date on mac:

brew install coreutils

That will install gdate executable 🙂