Skip to content

Instantly share code, notes, and snippets.

@nhtzr
Created October 23, 2019 03:41
Show Gist options
  • Save nhtzr/deac822a7118d29e9136cc3a858a96f4 to your computer and use it in GitHub Desktop.
Save nhtzr/deac822a7118d29e9136cc3a858a96f4 to your computer and use it in GitHub Desktop.
getenv with default in bash
#!/usr/bin/env bash
set -euo pipefail
key="${1:?Env var is required as first parameter}"
default=${2:-}
value="${!key:-$default}"
: "${value:?Env var $key not found}"
printf "%s" "${value}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment