Forcing bash to expand variables in a string loaded from a file

I stumbled on what I think is THE answer to this question: the envsubst command.

envsubst < something.txt

Example: To substitute variables in file source.txt and write it to destination.txt for further processing

envsubst < "source.txt" > "destination.txt"

In case it’s not already available in your distro, it’s in the
GNU package gettext.

@Rockallite

  • I wrote a little wrapper script to take care of the ‘$’ problem.

(BTW, there is a “feature” of envsubst, explained at
https://unix.stackexchange.com/a/294400/7088
for expanding only some of the variables in the input, but I
agree that escaping the exceptions is much more convenient.)

Here’s my script:

#! /bin/bash
      ## -*-Shell-Script-*-
CmdName=${0##*/}
Usage="usage: $CmdName runs envsubst, but allows '\$' to  keep variables from
    being expanded.
  With option   -sl   '\$' keeps the back-slash.
  Default is to replace  '\$' with '$'
"

if [[ $1 = -h ]]  ;then echo -e >&2  "$Usage" ; exit 1 ;fi
if [[ $1 = -sl ]] ;then  sl="\"  ; shift ;fi

sed 's/\\\$/\${EnVsUbDolR}/g' |  EnVsUbDolR=$sl\$  envsubst  "$@"

Leave a Comment