Skip to content

Instantly share code, notes, and snippets.

@tralston
Created March 29, 2019 07:19
Show Gist options
  • Save tralston/8fbce4cc26ae72fd86d25f201692994f to your computer and use it in GitHub Desktop.
Save tralston/8fbce4cc26ae72fd86d25f201692994f to your computer and use it in GitHub Desktop.
Choose among multiple similar commands in bash script
#!/bin/bash
# If the functionality of a GNU command (vs BSD) is needed, instead of checking for $OSTYPE, check if command is installed.
# This method isn't perfect, because you could have the case where the preferred command isn't installed when you expect it to be
# (e.g. Mac has no gdate installed). Perhaps in combination with $OSTYPE for better coverage.
# This checks if gdate is installed (likely beacuse running in a Mac environment). If not, use the normal date command.
DATE=$(type -p gdate date | head -n 1)
# Execute the command
$DATE +%Y%m%d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment