2

I did the following from a bash shell.

export myPath="/home/user/dir"

Then I verified the value of this by 'echo'ing this value from both shell and a inside a bash script. Both worked fine.

Then I tried setting the value of this variable from inside a script like this. myPath="/home/user/newdir" and tried printing this variable from shell. I thought the variable will hold the updated value, but it was showing the old value.

How can I update the value from a script? I am looking to do it without using source if possible.

1 Answer 1

6

To make the variables persist after the script finishes, you have to run it using the source command:

When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script.

Sign up to request clarification or add additional context in comments.

1 Comment

The files are part of a large code base that I don't understand completely. So I am not sure about any potential issues that may occur if I source this file. Pls share if you are aware of any alternatives. Thanks.

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.