0

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
  • This looks like a XY problem, is it? Commented Apr 29, 2020 at 20:07

2 Answers 2

3

You may use this bash substitution:

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

or else, if you have envsubst utility then use:

export MYDOMAIN
envsubst <<< "$REDIRECT"
Sign up to request clarification or add additional context in comments.

Comments

0

Just execute in bash:

eval REDIRECT=$REDIRECT

2 Comments

And let's pray that this eval won't fall in the wrong hands. Well, much better not to trust it.
eval tends to be unpredictable and unsafe unless you have good control over the data being evaluated. For example, does your URL have any & characters (i.e. multiple parameters) in it? If so, that'll be treated as a delimiter between separate shell commands rather than just part of a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.