I stumbled onto the question Replacing only specific variables with envsubst and in one of the comments was this shell example:
envsubst "$(printf '${%s} ' ${!PREFIX*})" < infile
See the original question for the full background, but in essence it is telling envsubst to replace only those environment variables that begin with the string "PREFIX".
But how is this working? Can someone explain the different "steps" that are involved? Is the infile read first? And what part is doing the grep/find kind of thing, so it can use the input from the file? Is the argument "${!PREFIX*}" some kind of regex? And is there some sub-shell magic being done here?
In order to try understanding it better, I created my own infile with this content:
1: ${VAR1}
2: ${VAR2}
3: ${VAR3}
4: ${PREFIX_1}
5: ${PREFIX_2}
6: ${PREFIX_3} ?
7: ${PREFIX_4} + ${PREFIX_5}
8: ${VAR4}
9: bla bla bla
I then replaced the envsubst with echo, resulting in this line:
echo "$(printf '${%s} ' ${!PREFIX*})" < infile
And this result:
${PREFIX_1}
So it works, but only for the first match. What am I doing wrong? I expected the output to contain all the "PREFIX_" variables 1 to 5. Maybe I messed up when I replaced envsubst with echo? If so, how can I see what stuff is sent to envsubst?
echo ${!PREFIX*}
andprintf '${%s} ' ${!PREFIX*}
, then e.g.echo ${!P*}
andprintf '${%s} ' ${!P*}
. – Bodo Jul 22 '19 at 11:27