Setting environment variable in shell script does not make it visible to the shell

后端 未结 2 1551
失恋的感觉
失恋的感觉 2020-12-08 14:27

I want to use a shell script that I can call to set some environment variables. However, after the execution of the script, I don\'t see the environment variable using \"pri

2条回答
  •  悲&欢浪女
    2020-12-08 14:35

    This is how environment variables work. Every process has a copy of the environment. Any changes that the process makes to its copy propagate to the process's children. They do not, however, propagate to the process's parent.

    One way to get around this is by using the source command:

    source ./test.sh
    

    or

    . ./test.sh
    

    (the two forms are synonymous).

    When you do this, instead of running the script in a sub-shell, bash will execute each command in the script as if it were typed at the prompt.

提交回复
热议问题