Keep vars environment after shell script ends

How do I keep environment variables set from a shell script after the script finishes running?

+3


source to share


3 answers


This is not possible by running the script. The script creates its own subordinate shell, which is lost when the script terminates.

To save export

what your script may have, you can call them like this to add them to your current environment:



. myScript.sh

Note the space between the .

and sections myScript.sh

.

+7


source


run your script like this:

source <script>

-OR-

. <script>

      



This will run the script in the current shell and define the variables in the current shell. Then the variables will be saved in the current shell after the script is executed.

+1


source


Environment variables represent an environment that is private to each process. The shell passes the COPY of the exported variables into it as its environment, but there is no way to return their environment to parents or other processes other than their children. You can print these variables and load them into the parent. Or, as mentioned, you can run the script in the current shell using "source script" or ".w371>" (and you may need. / Script if not in your TRACK). Some instruments print their vars, and the shell can load them using reverse ticks, for example ssh-agent

. This way, any ssh-agent prints will run as a command. If it prints something like "VAR1 = VAL; VAR2 = VAL2" which might do what you want.

+1


source







All Articles