Shell 脚本中设置环境变量
Shell 脚本中设置环境变量
正常情况下,向 ~/.bashrc 文件添加环境变量,然后在终端执行 source ~/.bashrc 命令即可生效。
但如果想在 shell 脚本当中完成环境变量的添加,并且 在当前终端立马生效 的话就必须在执行脚本的时候使用 source ,下面举个例子:
以下是脚本 test.sh 的内容
1 | #!/bin/bash |
执行脚本
1 | $ ./test.sh |
通常使用 . (source 缩写) 去执行的话,环境变量是不生效的;
必须使用 source 执行脚本,因为 shell 进程号和当前终端的进程号不一致。
使用 source 执行脚本
1 | $ source test.sh |
参考链接
- https://www.v2ex.com/t/350007
- https://zh.wikipedia.org/wiki/._(Unix)
- https://zh.wikipedia.org/wiki/Source_(%E5%91%BD%E4%BB%A4)