Skip to content

Instantly share code, notes, and snippets.

@darkfeline
Last active December 27, 2022 15:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkfeline/19a91aa9e59259bb61e4614a32091600 to your computer and use it in GitHub Desktop.
Save darkfeline/19a91aa9e59259bb61e4614a32091600 to your computer and use it in GitHub Desktop.
POSIX portable bash script
if [ -z "${BASH_VERSION}" ]; then
exec bash "$@"
fi
# Bash script here
echo "${BASH_VERSION}"
@mentalisttraceur
Copy link

Pretty good, so long as no code exports BASH_VERSION into environment variables (which is a bad idea in most cases, but nearly inevitable given enough users, uses, and use-cases).

Could protect against that case by adding another if like this:

if test -n "`printenv BASH_VERSION`"
then
    unset BASH_VERSION
    exec bash "$@"
fi

(To support ancient shells it would be best to replace unset BASH_VERSION with

BASH_VERSION=
export BASH_VERSION

but for anything from the last decade or two it's probably overkill.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment