Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active November 23, 2022 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scivision/6e6221a6ae6ad338dd257d6a689c5a6e to your computer and use it in GitHub Desktop.
Save scivision/6e6221a6ae6ad338dd257d6a689c5a6e to your computer and use it in GitHub Desktop.
Detect if shell is Bash or other

Not every shell has the "source" command. For example, in Dash (default for Ubuntu "sh"), the "./" is required in front of the filename.

. ./not_bash.sh
# source it or copy it into your script to avoid invoking a different shell. That's why there's no shebang.
case "$(type declare)" in
"declare is a shell builtin") echo bash;;
"declare is a reserved word") echo zsh;;
esac
# source it or copy it into your script to avoid invoking a different shell. That's why there's no shebang.
case "$(type declare)" in
"declare is a shell builtin") ;;
*) echo "Please use Bash shell"; return 1;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment