这个问题和这个问题类似: Is it possible to print the content of a variable of a variable with shell script? (间接引用)
在一个文件中
a=${env1}is a good ${env2}
b=${env3}is a good ${env2}
我想将此文件的内容显示给:
a=tom is a good cat
b=jerry is a good mouse
我试过这个:
tmp=`cat a.conf`
echo $tmp # environmental variables are not interpreted, and file format changed, hard to read
echo ${!tmp} # no ...
此外,上述想法有点绕道。
如果你有一个看起来像这样的文件:
如果您想生成替换变量的输出,请使用该
envsubst
命令,该命令是gettext
包的一部分。假设上面是 inexample.txt.in
,我们可以运行:并作为输出:
如果
ensubst
不可用,您可以执行以下操作:将脚本命名为
envsubst.sh
,并与前面的示例类似地运行它:env1=tom env2=cat env3=jerry env4=mouse sh envsubst.sh < example.txt.in