修改环境变量

时间:2022-06-11
本文章向大家介绍修改环境变量,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

暂时

通过 Shell 命令 export 直接修改 Linux 环境变量 使用 export 设置的变量,只对当前终端 Shell 有效 适合设置一些临时变量

sudo export PATH=$PATH:/usr/local/hadoop/bin

echo $PATH

来查看环境配置信息


永久

全局环境变量,设置的是所有用户的环境

/etc/profile /etc/bashrc /etc/environment

全局环境变量,设置的是整个系统的环境

/etc/environment

只对单个用户生效,当用户登录时该文件仅执行一次

~/.bash_profile ~/.profile

用户可使用该文件添加自己使用的 shell 变量信息 另外在不同的LINUX操作系统下,这个文件可能是不同的 可能是

~/.bash_profile ~/.bash_login ~/.profile

其中的一种或几种 如果存在几种的话,那么执行的顺序便是 ~/.bash_profile、 ~/.bash_login、 ~/.profile 比如 Ubuntu 系统一般是 ~/.profile 文件

只对单个用户生效,当登录以及每次打开新的 shell 时,该文件被读取

~/.bashrc

sudo gedit ~/.profile(or .bashrc) 

修改内容

export PATH=/usr/local/cuda/lib64:$PATH

or

PATH=/usr/local/cuda/bin:$PATH export PATH

保存设置

source profile

Problem& Solution

Problem_0

在 /etc/profile 下修改的路径,source 完 /etc/profile 后, 关闭当前进程, 却发现新写的路径在 新的 进程窗口 中无法被读入

Solution

原因未知

~/.bashrc 文件末尾添上一句话 :

source /etc/profile

source 该文件 :

source ~/.bashrc 

这样每次启动该用户,都会 自动 source 一遍 /etc/profile