一:echo $PATH
查看当前环境变量
二:设置环境变量的三种方法
1.直接在shell中输入 PATH=$PATH:<PATH1>:<PATH2>
这种方式设置的PATH变量只在当前shell或其子shell下是有效的,shell关闭了,PATH变量也就失效了,再打开新shell时需要重新设置该变量。
2.修改~/.bash_profile文件
在该文件中添加 export PATH=$PATH:<PATH1>:<PATH2>
(1)在shell中输入命令 source ~/.bash_profile 可以是该修正在该shell中有效。
source filename [arguments]
Read and execute commands from filename in the current shell environment and return the exit status of
the last command executed from filename.
(2)用户重新登录后修正生效。
3.修改/etc/profile文件
修改方式及使之生效同方式2。
三:other
(1)/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置。
(2)/etc/bashrc: 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取。
(3)~/.bash_profile: 每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。
(4)~/.bashrc: 该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。
(5) ~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件. 另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承 /etc/profile中的变量,他们是"父子"关系。
(6)~/.bash_profile 是交互式、login 方式进入 bash 运行的~/.bashrc 是交互式 non-login 方式进入 bash 运行的通常二者设置大致相同,所以通常前者会调用后者。
(7)在登录linux时文件/etc/profile,/etc/bashrc,~/.bash_profile,~/.bash_login,~/.profile,~/.bash_logout的执行顺序:
/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout。