cat /file | replace-all-env-vars > /file
# Or:
replace-all-env-vars /file > /file
function replace-all-env-vars () {
# Work with either `cat /file | replace-all-env-vars` and
# `replace-all-env-vars /file`
[ $# -ge 1 -a -f "$1" ] && input="$1" || input="-"
local contents=$(cat $input)
# Loop through each environment variable name
for v in $(env | cut -d '=' -f1); do
# Only look for things like `__ENV_PATH__`
local needle="__ENV_${v}__"
if [[ "${contents}" =~ "${needle}" ]]; then
# Replace all instances of needle with the value of `$v`
contents="${contents//$needle/${!v}}"
fi
done
echo "${contents}"
}