Skip to content

Instantly share code, notes, and snippets.

@guillermodlpa
Created March 7, 2023 13:25
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 guillermodlpa/11b0425add597a38a1a6e381d5175638 to your computer and use it in GitHub Desktop.
Save guillermodlpa/11b0425add597a38a1a6e381d5175638 to your computer and use it in GitHub Desktop.
Exports a list of env vars to the environment, supporting spaces, newline chars and quotes in the value
#!/bin/sh
# wraps the env var values in quotes and handles newlines
# It's a more sohisticated version from just doing export $($ENV_VARS)
# and it doesn't break with newline characters, quotes or spaces in the values (note the encoding with sed)
eval "$(
printf '%s\n' "$ENV_VARS" | while IFS='' read -r line; do
key=$(printf '%s\n' "$line"| sed 's/"/\\"/g' | cut -d '=' -f 1)
value=$(printf '%s\n' "$line" | cut -d '=' -f 2- | sed 's/"/\\\"/g')
printf '%s\n' "export $key=\"$value\""
done
)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment