bash用变量值替换字符串中的变量名 [英] bash replace variable name in string with variable value
问题描述
这有点奇怪.我有以下字符串:
This is kind of a weird one. I have the following string:
我有一个名为 REDIRECT 的变量设置为:https://working.${MYDOMAIN}/blah/blah
.
I have a variable called REDIRECT set to: https://working.${MYDOMAIN}/blah/blah
.
我需要将 ${MYDOMAIN} 替换为分配给 ${MYDOMAIN} 的变量的实际值.不确定 bash 还是 sed 最适合这个.我试过 bash replace 但无法让它工作,可能与转义字符或其他东西有关.任何帮助表示赞赏!
I need to replace the ${MYDOMAIN} with the actual value of the variable assigned to ${MYDOMAIN}. Not sure if bash or sed is best for this. I tried bash replace but couldn't get it to work, probably related to escaping the characters or something. Any help appreciated!
推荐答案
你可以使用这个 bash 替换:
You may use this bash substitution:
echo "${REDIRECT/\${MYDOMAIN\}/$MYDOMAIN}"
否则,如果您有 envsubst
实用程序,请使用:
or else, if you have envsubst
utility then use:
export MYDOMAIN
envsubst <<< "$REDIRECT"
这篇关于bash用变量值替换字符串中的变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!