Skip to content

Instantly share code, notes, and snippets.

@empeje
Last active June 4, 2019 13:08
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 empeje/257128cb16ffa17211cde7d6c7bd8a65 to your computer and use it in GitHub Desktop.
Save empeje/257128cb16ffa17211cde7d6c7bd8a65 to your computer and use it in GitHub Desktop.
Fallback Environment Variable Bash
#!/usr/bin/env bash
fallback_env() {
local TARGET_VARIABLE="$1"
local FALLBACK_VALUE="$2"
local CURRENT_VALUE=$(env | grep "^${TARGET_VARIABLE}=" | awk -F '=' '{print $2}')
if [[ -z "$CURRENT_VALUE" ]]; then
build_message "${TARGET_VARIABLE} is not set and will defaulting to ${FALLBACK_VALUE}"
eval "export ${TARGET_VARIABLE}=${FALLBACK_VALUE}"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment