我偶然发现了我认为该问题的答案:
envsubst命令。
envsubst < something.txt
如果您的发行版中尚未提供该功能,请在
GNU包gettext
。
@Rockallite-我写了一些包装脚本来解决’ $’问题。
(顺便说一句,有一个envsubst的“功能”,在https://unix.stackexchange.com/a/294400/7088上
进行了解释, 该扩展仅用于扩展输入中的某些变量,但是我同意转义异常的功能更多方便。)
这是我的脚本:
#! /bin/bash ## -*-Shell-script-*-CmdName=${0##*/}Usage="usage: $CmdName runs envsubst, but allows '$' to keep variables from being expanded. With option -sl '$' keeps the back-slash. Default is to replace '$' with '$'"if [[ $1 = -h ]] ;then echo -e >&2 "$Usage" ; exit 1 ;fiif [[ $1 = -sl ]] ;then sl='' ; shift ;fised 's/\$/${EnVsUbDolR}/g' | EnVsUbDolR=$sl$ envsubst "$@"


