bash envsubst 有转义字符吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24963705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is there an escape character for envsubst?
提问by quinn
Is there a way to prevent envsubst
from substituting a $VARIABLE
? For example, I would expect something like:
有没有办法防止envsubst
替换 a $VARIABLE
?例如,我希望是这样的:
export THIS=THAT
echo "dont substitute \$THIS" | envsubst
and have it return
并让它返回
dont substitute $THIS
but instead I get
但我得到
dont substitute \THAT
is there any escape character for doing this?
这样做有什么转义字符吗?
采纳答案by iokanuon
export DOLLAR='$'
export THIS=THAT
echo '${DOLLAR}THIS' | envsubst
Or more clear:
或者更清楚:
export THIS=THAT
echo '${DOLLAR}THIS' | DOLLAR='$' envsubst
回答by chepner
If you give envsubst
a list of variables, it onlysubstitutes those variables, ignoring other substitutions. I'm not exactly sure how it works, but something like the following seems to do what you want:
如果您提供envsubst
变量列表,它只会替换这些变量,而忽略其他替换。我不确定它是如何工作的,但类似以下的内容似乎可以满足您的需求:
$ export THIS=THAT FOO=BAR
$ echo 'dont substitute $THIS but do substitute $FOO' | envsubst '$FOO'
dont substitute $THIS but do substitute BAR
Note that $THIS
is left alone, but $FOO
is replaced by BAR
.
请注意,$THIS
保持不变,但$FOO
被替换为BAR
。
回答by blueFast
My workaround is as follows:
我的解决方法如下:
Original template:
原始模板:
$change_this
$dont_change_this
Editted template:
编辑模板:
$change_this
§dont_change_this
Now you can process:
现在您可以处理:
envsubst < $template | sed -e 's/§/$/g'
This relies on the character §
not occurring anywhere else on your template. You can use any other character.
这依赖于§
没有出现在模板上其他任何地方的字符。您可以使用任何其他字符。
回答by user2658308
In my case I wanted to only escape vars that aren't already defined. To do so run:
就我而言,我只想转义尚未定义的变量。为此,请运行:
envsubst "$(env | sed -e 's/=.*//' -e 's/^/\$/g')"
envsubst "$(env | sed -e 's/=.*//' -e 's/^/\$/g')"
回答by Malvineous
If there's only one or two variables you don't want to expand, you can sort of whitelist them by temporarily setting them to their own name, like this:
如果只有一两个变量您不想扩展,您可以通过将它们临时设置为自己的名称来将它们列入白名单,如下所示:
$ echo 'one $two three $four' | four='$four' envsubst
one three $four
Here, the $four
variable gets replaced with $four
, effectively leaving it unchanged.
在这里,$four
变量被替换为$four
,有效地保持不变。
回答by Lech Paw?aszek
$?echo $SHELL
/bin/bash
$ echo $SHELL
$SHELL
$ echo $SHELL | envsubst
/bin/bash
$ echo $${q}SHELL | envsubst
$SHELL
So doing $$
allows you to add a $
character. Then just "substitute" non-existent variable (here I used ${q}
but can be something more meaningful like ${my_empty_variable}
and you'll end up with what you need.
这样做$$
可以让您添加一个$
字符。然后只是“替换”不存在的变量(这里我使用了${q}
但可以更有意义的东西${my_empty_variable}
,你最终会得到你需要的。
Just as with the paragraph solution - you need something special - here... a non-existent variable, which I like a bit more than performing additional sed on templates.
就像段落解决方案一样——你需要一些特别的东西——这里......一个不存在的变量,我更喜欢它而不是在模板上执行额外的 sed。
回答by admin
If you use envsubst
for Docker or you don't like his behavior, you can try my solution for replacing env variables in files: https://github.com/roquie/smalte
如果你使用envsubst
Docker 或者你不喜欢他的行为,你可以尝试我的解决方案来替换文件中的 env 变量:https: //github.com/roquie/smalte