Skip to content

Instantly share code, notes, and snippets.

@mmguero
Created September 4, 2020 13:31
Show Gist options
  • Save mmguero/007a129fe298ff9f1cf936c3914eb95e to your computer and use it in GitHub Desktop.
Save mmguero/007a129fe298ff9f1cf936c3914eb95e to your computer and use it in GitHub Desktop.
determine script path in a bash script
#!/usr/bin/env bash
[[ "$(uname -s)" = 'Darwin' ]] && REALPATH=grealpath || REALPATH=realpath
[[ "$(uname -s)" = 'Darwin' ]] && DIRNAME=gdirname || DIRNAME=dirname
if ! (type "$REALPATH" && type "$DIRNAME") > /dev/null; then
echo "$(basename "${BASH_SOURCE[0]}") requires $REALPATH and $DIRNAME"
exit 1
fi
SCRIPT_PATH="$($DIRNAME $($REALPATH -e "${BASH_SOURCE[0]}"))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment