我想在 shell 中永久设置一个全局环境变量,因此我不会在每次打开另一个 shell 或另一个登录 session 时都设置它。
我已使用 export
设置变量如下:
$ export pass='my_pass'
但是,当我使用另一个事件 shell 来使用 echo
恢复此变量时如下:
$ echo $pass
该变量不存在,因此仅存在于 setup 的本地 shell 中。
我尝试将其放入 .bash_profile
但这也不起作用。
最佳答案
~/.bash_profile
仅在登录时获取(即在您输入用户名和密码之后) - ~/.bashrc
用于交互式非登录 shell。
因此,我会将变量添加到 ~/.bashrc
中(如果您随后从同一 shell 运行 python 脚本,请不要忘记首先source
它)。这样,当您打开新的 shell 时,将获取 bashrc 并且您的环境变量将可用。
编辑:
正如其他人在评论中所说..在一个 shell 中运行导出命令,不会使该变量在另一个 shell 中可用 - 您需要将其添加到您的 ~/.bashrc
中以使其在其他 shell 中可用
关于linux - 在linux bash中永久设置环境变量的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58338149/