This is not ideal, in that it doesn't alter the behaviour of envsubst
as requested, but it is able to identify unset variables. The user must ensure that the delimiter EOF
does not appear in the text. If it does, then choose a different delimiter.
#!/usr/bin/env bash
msg="$( printf 'cat << EOF\n%s\nEOF\n' "$(cat)" )"
bash -u <<< "$msg"
Output:
$ ./test.sh < message.txt || echo fail
bash: line 1: LOCATION: unbound variable
fail
$ LOCATION=World ./test.sh < message.txt || echo fail
bash: line 1: PERSON: unbound variable
fail
$ LOCATION=World PERSON=Ralph ./test.sh < message.txt || echo fail
Hello, World! You too, Ralph ;)
Here is a lengthier version which will list all the unset variables in one go, instead of disclosing them one at a time:
#!/usr/bin/env bash
check_vars() {
# pass a list of variable names on stdin, one to a line
rc=0
while read v
do
if [[ ! "${!v}" ]]
then
printf '%s\n' "$v"
rc=1
fi
done
return $rc
}
envsubst -v "$(cat)" | check_vars
This version will output a list of unset (or null) variables, one to a line, and exit with 0 if and only if the list is empty.
Output:
$ ./test2.sh < message.txt || echo fail
LOCATION
PERSON
fail
$ PERSON=Ralph ./test2.sh < message.txt || echo fail
LOCATION
fail
$ LOCATION=World ./test2.sh < message.txt || echo fail
PERSON
fail
$ LOCATION=World PERSON=Ralph ./test2.sh < message.txt || echo fail
$
false
, but I guess I could work with anything reliable. I'll edit the question.envsubst -no-unset
is seems that we want, but it is not original GNU gettext implementation, it is new implementation written in Golang.