Skip to content

Instantly share code, notes, and snippets.

@zerolagtime
Created June 1, 2020 20:01
Show Gist options
  • Save zerolagtime/1fb8507903fae30662fa31303d81cd38 to your computer and use it in GitHub Desktop.
Save zerolagtime/1fb8507903fae30662fa31303d81cd38 to your computer and use it in GitHub Desktop.
envsubst in pure bash/sed
#!/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