How do I substitute environment variables when I ouput a file?
Old thread, but this is a recurrent issue.
Here is a solution using bash's mapfile
:
mateus@mateus:/tmp$ cat input.txt
a = $a
b = $b
mateus@mateus:/tmp$ echo a=$a, b=$b
a=1, b=2
mateus@mateus:/tmp$ function subst() { eval echo -E "$2"; }
mateus@mateus:/tmp$ mapfile -c 1 -C subst < input.txt
a = 1
b = 2
The bash builtin mapfile
calls user-defined function subst
(see -C/-c options) on each
line read from input file input.txt
. Since the line contains unescaped text, eval
is used tu evaluate it and echo
the transformed text to stdout (-E avoid special characters to be interpreted).
IMHO this is a much more elegant than any sed/awk/perl/regex based solution.
Another possible solution is to use shell's own substitution. This looks like a more "portable" way not relaying on mapfile
:
mateus@mateus:/tmp$ EOF=EOF_$RANDOM; eval echo "\"$(cat <<$EOF
$(<input.txt)
$EOF
)\""
a = 1
b = 2
Note we use $EOF
to minimize conflicting cat's here-document with input.txt content.
Both examples from: https://gist.github.com/4288846
EDIT: Sorry, the first example doesn't handles comments and white space correctly. I'll work on that and post a solution. EDIT2: Fix double $RANDOM eval
As described in the same thread at http://www.issociate.de/board/post/281806/sed_replace_by_enviroment_var_content.html, the "proper" solution would be as follows:
awk '{while(match($0,"[$]{[^}]*}")) {var=substr($0,RSTART+2,RLENGTH -3);gsub("[$]{"var"}",ENVIRON[var])}}1' < input.txt > output.txt
eval
approach is fine, but tends to fail on XML files for instance.
Can be done with script (e.g. file name is expand.sh
):
while read line; do eval echo \"$line\"; done < $1 > $2
The script may be called like this:
env VAR1=value1 sh expand.sh input_file output_file
— http://www.issociate.de/board/post/281806/sed_replace_by_enviroment_var_content.html