0

I'm trying to replace the value of an environment variable definition, if it exists, from a bash script. I know I can use sed to do this, I'm not sure, however, how to replace the value of the environment variable?

Here's what I'd like to do:

Given a file with this line (found with grep):

export MY_ENV=SOME_VALUE

I'd like to replace SOME_VALUE with something else. How do I do that with sed?

2 Answers 2

5

This searches for the line beginning with export MY_ENV= and substitutes the rest of the line with NEW_VALUE:

sed 's/^\(export MY_ENV=\).*$/\1NEW_VALUE/'
Sign up to request clarification or add additional context in comments.

1 Comment

Also, sed -i will modify the file in-place.
0
sed -e "s/SOME_VALUE/$MY_ENV/"

This locates the string SOME_VALUE and substitutes the value of $MY_ENV. The double quotes are crucial; don't use single quotes unless you want $, M, Y, ... in the replacement text.

Comments

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.