问题
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