bash replace variable name in string with variable value

纵然是瞬间 提交于 2021-02-17 06:37:05

问题


This is kind of a weird one. I have the following string:

I have a variable called REDIRECT set to: https://working.${MYDOMAIN}/blah/blah.

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!


回答1:


You may use this bash substitution:

echo "${REDIRECT/\${MYDOMAIN\}/$MYDOMAIN}"

or else, if you have envsubst utility then use:

export MYDOMAIN
envsubst <<< "$REDIRECT"



回答2:


Just execute in bash:

eval REDIRECT=$REDIRECT


来源:https://stackoverflow.com/questions/61510099/bash-replace-variable-name-in-string-with-variable-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!