Skip to content

Instantly share code, notes, and snippets.

@StudioEtrange
Created March 28, 2018 23:06
Show Gist options
  • Select an option

  • Save StudioEtrange/49c437d94c265f811e68b528216819e5 to your computer and use it in GitHub Desktop.

Select an option

Save StudioEtrange/49c437d94c265f811e68b528216819e5 to your computer and use it in GitHub Desktop.
shell detect if sourced
# https://stackoverflow.com/a/28776166
sourced=0
if [ -n "$ZSH_EVAL_CONTEXT" ]; then
case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac
elif [ -n "$KSH_VERSION" ]; then
[ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] && sourced=1
elif [ -n "$BASH_VERSION" ]; then
[ "$0" != "$BASH_SOURCE" ] && sourced=1
else # All other shells: examine $0 for known shell binary filenames
# Detects `sh` and `dash`; add additional shell filenames as needed.
case ${0##*/} in sh|dash) sourced=1;; esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment