![全局变量不会从 .profile 自动加载](https://linux22.com/image/1204106/%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F%E4%B8%8D%E4%BC%9A%E4%BB%8E%20.profile%20%E8%87%AA%E5%8A%A8%E5%8A%A0%E8%BD%BD.png)
为了设置 CUDA 9.1,我了解到将其安装文件夹添加到 和 会很方便PATH
,LD_LIBRARY_PATH
如下所示:
PATH="/usr/local/cuda-9.1/bin:$PATH"
LD_LIBRARY_PATH="/usr/local/cuda-9.1/lib64:$LD_LIBRARY_PATH"
下列的这和这SE 回答我尝试编辑我的.profile
文件并添加如下所示的最后几行。
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin directories
PATH="$HOME/bin:$HOME/.local/bin:$PATH"
# settings for CUDA
PATH="/usr/local/cuda-9.1/bin:$PATH"
LD_LIBRARY_PATH="/usr/local/cuda-9.1/lib64:$LD_LIBRARY_PATH"
使用.profile
上述方法,我可以打开终端并运行echo $PATH
以查看指令是否有效,因为PATH
现在包含我指示的文件夹。但是,出于某种原因,这对 不起作用LD_LIBRARY_PATH
。
我猜测问题可能是LD_LIBRARY_PATH
以前不存在的,所以我尝试.profile
用下面最后两行略有不同的代码进行修改。
PATH="/usr/local/cuda-9.1/bin:$PATH"
export LD_LIBRARY_PATH=/usr/local/cuda-9.1/lib64
但再次失败了。
注意到 开头的第一个警告.profile
,我检查了是否有~/.bash_profile
或~/.bash_login
文件。它们不存在,而且无论如何它们都无法解释我的 如何PATH
成功更新。
做了一些研究后,我偶然发现这个其他答案解释了.profile
当我打开终端时不一定会执行。但是,我又该如何解释PATH
更新了呢?
可能是什么问题?我的语法有问题吗?
编辑:
我尝试注销并重新登录后,我改变了.profile
包含
PATH="/usr/local/cuda-9.1/bin:$PATH"
export LD_LIBRARY_PATH=/usr/local/cuda-9.1/lib64
现在它起作用了。如果我进入终端并输入,echo $LD_LIBRARY_PATH
我终于可以看到它了。我仍然不明白为什么我的指令列表的第一个版本不起作用......
答案1
之所以PATH
没有运行,是因为它在运行export
之前被设置为环境变量。~/.profile
改变现有的环境变量,
VAR=foo
足够了。
到添加一个环境变量,你需要做
export NEWVAR=bar
请参见环境变量进一步了解该主题。
答案2
我认为您的解决方案是不言自明的,export
关键字起到了作用,并且需要“导出”环境变量以供使用。您可以使用 export 命令导出 shell 变量。
要查看导出变量的列表,请运行export -p
看man bash
:
export [-fn] [name[=word]] ...
export -p
The supplied names are marked for automatic export to the environment of subsequently executed commands. If the -f option is given, the names
refer to functions. If no names are given, or if the -p option is supplied, a list of names of all exported variables is printed. The -n
option causes the export property to be removed from each name. If a variable name is followed by =word, the value of the variable is set to
word. export returns an exit status of 0 unless an invalid option is encountered, one of the names is not a valid shell variable name, or -f is
supplied with a name that is not a function.