Checking operating system in a bash script and installing different programs

echo "-- Checking your machine type..."

# Now we need to detect the platform we're running on (Linux / Mac / Other)
# so we can fetch the correct binary and place it in the correct location
# on the machine.

# We use "tr" to translate the uppercase "uname" output into lowercase
UNAME=$(uname -s | tr '[:upper:]' '[:lower:]')

# Then we map the output to the names used on the Github releases page
case "$UNAME" in
    linux*)     MACHINE=linux;;
    darwin*)    MACHINE=macos;;

# If we never define the $MACHINE variable (because our platform is neither Mac
# or Linux), then we can't finish our job, so just log out a helpful message
# and close.
if [ -z "$MACHINE" ]
    echo "Your operating system is not supported, if you think it should be please file a bug."
    echo ""
    echo "-- All done!"

    send_analytics_event "missing_platform_$UNAME"
    exit 0

I got this from Google's install script from Firebase CLI

  • uname -s prints the operating system name

Once you have the , you can use an if/esle or case statement

[[ $UNAME = darwin ]] && echo 'macOS' || echo 'Linux'

Please note that this site and the posts on it are, and will always be, a work in progress. If i waited for perfection, i’d never get anything done.