Member-only story

How to handle with problems with different commands on FreeBSD/Linux/macOS

Maciej
2 min readAug 11, 2021

--

Introduction

Due to the different commands between Linux (GNU coreutils) and FreeBSD, macOS, it is necessary to consider the environment in which the script is executed in the shell script. For example, if you want to get the last modified date and time of a file, you want stat to use it, but stat the flags that specify the output format are different between GNU coreutils, FreeBSD, and macOS.

I think it’s true that you can write in Perl or Python as a premise, but there are some things that you can’t do without shell scripts such as bash and zsh plugins. It’s a trivial matter if you just write it yourself and use it yourself, but it’s not so if it’s shared with others.

TL; DR

  • Take advantage of syntax differences (stat)
  • Substitute with a command with the same syntax (openssl)

Determine the environment by uname

There is a judgment by the most common coping method .

case "$(uname)"; in
Linux)
# GNU coreutils
;;
Darwin)
# macOS
;;
esac

This has one big problem: if you’re using GNU coreutils on macOS, the environment-based decision will fail in the first place. I’m using…

--

--

Maciej
Maciej

Written by Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.

No responses yet