Skip to content

Instantly share code, notes, and snippets.

@aamnah
Last active March 31, 2022 09:23
Show Gist options
  • Save aamnah/f5b3dbbdb3721cd907879f943cb6c30a to your computer and use it in GitHub Desktop.
Save aamnah/f5b3dbbdb3721cd907879f943cb6c30a to your computer and use it in GitHub Desktop.
Check operating system inside a bash script and change commands
# Flush DNS cache
if [[ $OSTYPE == darwin* ]]; then
# works on macOS
alias flushdns='sudo dscacheutil -flushcache'
elif [[ $OSTYPE == linux* ]]; then
# works on Ubuntu 18.04+
alias flushdns='sudo systemd-resolve --flush-caches'
fi
UNAME=$(uname -s | tr '[:upper:]' '[:lower:]')
# example if/esle statement 1
if [[ $UNAME = darwin ]]; then
echo "You are on macOS"
elif [[ $UNAME = linux ]]; then
echo "You are on Linux"
fi
# example if/esle statement 2
[[ $UNAME = darwin ]] && echo "You are on macOS" || echo "You are on macOS"
# example if/esle statement 3
if [[ $UNAME = darwin ]]; then echo "You are on macOS"; else echo "You are on macOS"; fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment