设置环境变量有3种方法
- 运行
export PATH=$PATH:[new_path]
- 更改
/usr/.bash_profile
,并source/usr/.bash_profile
- 更改
/etc/bash.bashrc
,并source /etc/bash.bashrc
(永久生效)
- ⚠️ bash_profile和bash.bashrc的路径可能在不同操作系统上不一样。例如在华为云服务器中,操作系统为Centos7.9,
.bash_profile
文件的绝对路径为/root/.bash_profile
,通过更改这个文件中对于环境变量$PATH
的设置,运行source/usr/.bash_profile
可以永远生效对环境变量的改正。可以通过find
locate
whereis
等操作在服务器中全局寻找某个文件。 - 如何判断环境变量的更改是否永久生效:新开一个终端,在新终端里运行
echo $PATH
打印查看是否永久生效。