Created
June 1, 2020 20:01
-
-
Save zerolagtime/1fb8507903fae30662fa31303d81cd38 to your computer and use it in GitHub Desktop.
envsubst in pure bash/sed
This file contains 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/bash | |
# this is a poor man's substitute for the envsubst command | |
function internal_envsubst() { | |
tfile=$(mktemp) | |
# use sed to build a sed input file specific to the current environment variables | |
env | sed -E -e ' | |
# delete any special cases that make a mess of things | |
/^_=/d; | |
# deal with variables that have backslashes in them: \\tsclient\home | |
s=\\=\\\\=g; | |
# our output will use equal signs for separators, so escape any on the input | |
s/=/\\=/g; | |
s/\\=/=/; | |
# transform each variable into a sed command for later execution | |
s%^([^=]*)=(.*)$%s=\\$\\{?\1\\}?([^_A-Za-z0-9]|$)=\2=g;% | |
' > $tfile | |
sed -E -f $tfile | |
rm $tfile | |
} | |
internal_envsubst |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment