Jeffrey
Jeffrey

Reputation: 4506

Why does /bin/sh behave differently on Mac and Ubuntu

I'm writing a sh file to get the modification time of a file. I want the sh file works on both Mac the Ubuntu.

I use the /bin/sh and add #!/bin/sh to the first line of the bash file. I suppose /bin/sh should behave the same on the two OS. But it doesn't. Below are the two examples for the differences.

  1. The script below works on Mac but not on Ubuntu.

    modTime=$(stat -f "%m" -t "%s" $filepath)

  2. And the script below works on Ubuntu but not on Mac.

    modTime=$(date +%s -r $filepath)

My questions are:

  1. Why the /bin/sh behaves differently on on Mac and Ubuntu?
  2. If I want to write the cross-platform sh script, how should I avoid the platform-dependent code?

Upvotes: 3

Views: 7366

Answers (2)

thom
thom

Reputation: 2332

  1. Why the /bin/sh behaves differently on on Mac and Ubuntu?

because they are both different shells. sh on ubuntu is the dash shell, sh on minix is the ash shell, sh on slackware is the bash shell. And since not too long is sh on OSX also the bash shell

If you want same behaviour, specify your shell... #!/bin/sh is, although strictly the never-in-free-systems-implemented-bourne-shell, generally the system shell which can vary from system to system ...use #!/bin/bash to literally say that you mean bash, use #!/bin/ksh to literally call the korn-shell etc.

  1. If I want to write the cross-platforma sh script, how should I avoid the platform-dependent code?
  1. officially: keep strict to the POSIX rules to run on any POSIX compliant shell

  2. pragmatically: write specific for one type of shell that is available on almost any system (not counting microcontrollers and SoC's or big irons and sparc stations, bash is available for most systems, but yes there are system who don't do bash so bash is not the best choice for 100% portability, but it is absolutely the most (ab)used ;-)

  3. best portable: As an Ubuntu user rule of thumb: if it runs on dash (the /bin/sh of ubuntu) it will run on virtually anything (including your router , your toaster and your coffeemachine).

  4. Above all: user2719058 is right, OSX is not Linux but BSD-UNIX so while they can run the same shell, the commands are just different enough to make it very very difficult to write a one-script-fits-all. Choosing the best shell will not change that....so the value of a unified scripting language is hereby proven to be very limited unless the system commands on every system are POSIX compliant as well.

tl;dr:
Unified cross-platform scripting is a pipe dream because differences in binaries across systems prevent this.

Upvotes: 7

user2719058
user2719058

Reputation: 2233

This is not the shell behaving differently, but the commands you are running. On Ubuntu, these come from the GNU world, on MacOS, they are probably some BSD variant. Which usually means, on MacOS you have fewer features. :)

It should be noted that this (I guess) applies to /bin/sh as well, but that's not the reason for the breakage you see.

Upvotes: 4

Related Questions