Created
March 7, 2023 13:25
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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