Powered by Typecho)))
Optimized by EAimTY
经常做各种配置模板时,需要动态生成配置文件. 用别的脚本语言实现太大杀器了.
envsubst
命令就能实现简单的变量替换
比如我们现在有一个简单的toml配置模板config.toml.tpl:
[main]
baseURL = "${URL}"
title = "${TITLE}"
[user]
然后申明环境变量
export URL=https://www.qq.com
export TITLE="tencent inc"
后执行
envsubst < config.toml.tpl > config.toml
, 将会根据tpl模板内容自动替换对应变量生成文件输出到config.toml
也可以直接引入变量
URL=https://www.qq.com TITLE="tencent inc" envsubst < config.toml.tpl > config.toml
查看生成内容:
[main]
baseURL = "https://www.qq.com"
title = "tencent inc"
如果只想替换某个单独的变量,可以通过参数指定,如只替换
${TITLE}
:
envsubst '${TITLE}'< config.toml.tpl > config.toml