1.查看环境变量
(1).查看单个环境变量
[root@server3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
(2)查看所有环境变量
[root@server3 ~]# env
(3)查看所有本地定义的环境变量
[root@server3 ~]# set
2.设置环境变量
(1).export命令
生效方法:立即生效
有效期限:临时改变,只能在当前的终端窗口中有效,当前窗口关闭后就会恢复原有的path配置
用户局限:当前用户
[root@server3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@server3 ~]# export PATH=/usr/local/nginx/bin:$PATH
[root@server3 ~]# echo $PATH
/usr/local/nginx/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#还原:
[root@server3 ~]# exit
logout
Connection to 172.25.66.3 closed.
[root@foundation66 bin]# ssh root@172.25.66.3
root@172.25.66.3's password:
Last login: Sun Apr 14 10:40:18 2019 from foundation66.localdomain
[root@server3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
(2).修改~/.bashrc文件
生效方法:1.关闭当前终端窗口,重新打开一个新终端窗口就能生效
2.输入“source ~/.bashrc”命令,立即生效
有效期限:永久有效
用户局限:当前用户
[root@server3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# ~ 即为 /root
[root@server3 ~]# vim ~/.bashrc
#################
export PATH=/usr/local/nginx/bin:$PATH
[root@server3 ~]# source ~/.bashrc
[root@server3 ~]# echo $PATH
/usr/local/nginx/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@server3 ~]# exit
logout
Connection to 172.25.66.3 closed.
[root@foundation66 bin]# ssh root@172.25.66.3
root@172.25.66.3's password:
Last login: Sun Apr 14 10:46:22 2019 from foundation66.localdomain
[root@server3 ~]# echo $PATH
/usr/local/nginx/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#还原:
[root@server3 ~]# vim ~/.bashrc
################
删除: export PATH=/usr/local/nginx/bin:$PATH
[root@server3 ~]# source ~/.bashrc
[root@server3 ~]# echo $PATH
/usr/local/nginx/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@server3 ~]# exit
logout
Connection to 172.25.66.3 closed.
[root@foundation66 bin]# ssh root@172.25.66.3
root@172.25.66.3's password:
Last login: Sun Apr 14 10:55:54 2019 from foundation66.localdomain
[root@server3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
(3)修改.bash_profile文件
生效方法:source命令
有效期限:永久有效
用户局限:当前用户
[root@server3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@server3 ~]# vim .bash_profile
#################
添加 :/usr/local/nginx/bin
[root@server3 ~]# source .bash_profile
[root@server3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/nginx/bin
[root@server3 ~]# exit
logout
Connection to 172.25.66.3 closed.
[root@foundation66 bin]# ssh root@172.25.66.3
root@172.25.66.3's password:
Last login: Sun Apr 14 11:05:47 2019 from foundation66.localdomain
[root@server3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/bin
#还原:
[root@server3 ~]# vim .bash_profile
#################
删除 :/usr/local/nginx/bin
[root@server3 ~]# source .bash_profile
[root@server3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/bin:/root/bin
[root@server3 ~]# exit
logout
Connection to 172.25.66.3 closed.
[root@foundation66 bin]# ssh root@172.25.66.3
root@172.25.66.3's password:
Last login: Sun Apr 14 11:22:46 2019 from foundation66.localdomain
[root@server3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
(4)修改/etc/profile文件
生效方法:source命令
有效期限:永久有效
用户局限:对所有用户
[root@server3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@server3 ~]# vim /etc/profile
#################
export PATH=/usr/local/nginx/bin:$PATH
[root@server3 ~]# source /etc/profile
[root@server3 ~]# echo $PATH
/usr/local/nginx/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#还原:
[root@server3 ~]# vim /etc/profile
#################
删除: export PATH=/usr/local/nginx/bin:$PATH
[root@server3 ~]# source /etc/profile
[root@server3 ~]# echo $PATH
/usr/local/nginx/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@server3 ~]# exit
logout
Connection to 172.25.66.3 closed.
[root@foundation66 bin]# ssh root@172.25.66.3
root@172.25.66.3's password:
Last login: Sun Apr 14 10:59:01 2019 from foundation66.localdomain
[root@server3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
3.常用的环境变量
PATH #决定了shell将到哪些目录中寻找命令或程序
HOME #当前用户主目录
HISTSIZE #历史记录数
LOGNAME #当前用户的登录名
HOSTNAME #指主机的名称
SHELL #当前用户Shell类型
LANGUGE #语言相关的环境变量,多语言可以修改此环境变量
MAIL #当前用户的邮件存放目录
PS1 #基本提示符,对于root用户是#,对于普通用户是$