Multi-platform shell scripts

Published in Snippets on Oct 15, 2021

While Linux and MacOS share a lot of similar terminal commands, some things remain different. Package management, for example. On Ubuntu, you commonly use APT, on MacOS, many use Brew. When you're writing a shell script that needs to run on both platforms, this poses a problem.

I recently had to write a "multi-platform" shell script and used this little code fragment:

#!/bin/bash

system_type=$(uname -s)

if [ "$system_type" = "Linux" ]; then
  sudo apt-get -y install vim
elif [ "$system_type" = "Darwin" ]; then
  brew install vim
else
  echo "Panic!"  
fi

Simple and effective. For anyone running into the same problem, here you go!