Linux 中设置环境变量的三种方法

1.export 变量名=value
2.变量名=value ; export 变量名
3.declare -x 变量名=value

自定义全局环境变量

1.编辑 /etc/profile
vim /etc/profile
Linux 中设置环境变量的三种方法
2.使定义的全局环境变量生效
root@raspberrypi:~# source /etc/profile
或者
root@raspberrypi:~# . /etc/profile
3.验证
Linux 中设置环境变量的三种方法

让环境变量永久生效的常用设置文件

(1.) 用户的环境变量配置

对于用户的 环境变量设置,比较常见的是用户家目录下的 ./.bashrc 和 ./.bash_profile 这两个文件。一般推荐在 .bashrc 中优先设置

(2.)全局环境变量的配置

常见的全局环境变量的配置文件有:/etc/profile /etc/bashrc /etc/profile.d 。一般推荐在 /etc/bashrc 里优先设置。

  • 若要在登陆后初始化或显示加载内容,则把脚本文件放在/etc/profile.d/下即可(无需加可执行权限)