简体   繁体   中英

Changing the value of a export variable from a bash script

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM