https://www.csdn.net/tags/MtzaEg0sMzcyNzctYmxvZwO0O0OO0O0O.html
环境变量——bash shell使用环境变量来存储系统相关数据,并允许将数据存储在内存中。
环境变量分为:全局环境变量
本地环境变量
目录全局环境变量
本地环境变量
设置全局环境变量
删除环境变量
PATH全局环境变量
设置系统环境变量的相关文件(登录、非登录、交互、非交互shell)
一、全局环境变量
在当前shell和子shell都可见
可以用printenv命令查看全局环境变量,大写表示是系统环境变量,小写表示是普通用户的环境变量
这是bash shell的一个标准约定,不是必须的,因此在设置新的环境变量的时候我们用小写就行了,用于区分个人和系统环境变量。
[root@CentOS6 ~]# printenv
TERM=linux
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=172.18.251.124 8132 22
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/4
name=hello # 自己定义的环境变量
USER=root
LS_COLORS=…
MAIL=/var/spool/mail/root
PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/root
LANG=en_US.UTF-8
PS1=[[\e[33m]\u@[\e[34m]\h [\e[m]\W]$ [\e[m]
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HISTCONTROL=ignoredups
PS2=[\e[34m]> [\e[m]
SHLVL=1
HOME=/root
LOGNAME=root
QTLIB=/usr/lib64/qt-3.3/lib
CVS_RSH=ssh
SSH_CONNECTION=172.18.251.124 8132 172.18.250.183 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
DISPLAY=localhost:12.0
G_BROKEN_FILENAMES=1
_=/usr/bin/printenv
大部分变量都是在登录主shell时设置的
二、本地环境变量
只在当前shell中可见
可以通过set命令查看,不过set命令查看的是所有环境变量(全局和本地)
注意在设置环境变量的时候,[变量=值]之间不能添加空格,要不然shell会把它当做一个单独的命令执行
99a69abe4dd839fb9a50983a4613602c.png
三、设置全局环境变量
使用export命令将本地环境变量变为全局环境变量
c31d75ad3928b0b8a8ec171d9e34fab0.png
四、删除环境变量
使用unset命令可以删除环境变量,格式为
unset 变量名
不过对于全局环境变量的删除,我们要注意:
如果在子shell下删除全局环境变量,删除操作只对子shell有效,如果回到父shell下,该全局变量还能引用
五、PATH全局环境变量
修改PATH环境变量:
PATH=$PATH:新加目录
小技巧:
我们可以将PATH设置为PATH=$PATH:.(单个点代表当前工作目录)
六、设置系统环境变量的相关文件
系统环境变量是在shell启动过程中执行相关的文件定义的。这些文件被称为shell启动文件。不过我们在设置系统环境变量的时候,我们要区分登录式shell、非登录式shell、交互式shell、非交互式shell的区别,(登录/非登录和交互/非交互只是划分的标准不一样)只有弄清除了不同模式的shell才能正确修改相应的shell启动文件以至于能够正确设置系统环境变量。
正好最近也在接触Linux系统启动流程,这也会涉及到登录一个shell的过程。
6.1 登录式shell
登录式shell是用户需要输入用户名和密码的shell,该模式的shell启动过程中会依次执行下列文件,
/etc/profile # 登录bash shell的默认主启动文件。任何用户登录shell都会执行此启动文件。不建议修改
~/.bash_profile
~/.bash_login
~/.profile # 上诉这三个$HOME启动文件是定义对应用户的环境变量。不同linux发行版使用的文件不同
/etc/profile中的命令和脚本不是我们现在关注的,我们主要来看看export那一行,因此我们可以知道该文件是设置系统全局环境变量
/etc/profile另一个重要的功能就是能够重复执行/etc/profile.d/目录下的文件(大多是.sh和.csh结尾的文件),这些文件大概是特定应用程序的启动文件,能够设置相关应用程序的环境变量,例如/etc/profile.d/lang.*sh 就是用来设置LANG环境变量的。[root@CentOS6 ~]# cat /etc/profile
/etc/profile
System wide environment and startup programs, for login setup
Functions and aliases go in /etc/bashrc
It’s NOT a good idea to change this file unless you know what you
are doing. It’s much better to create a custom.sh shell script in
/etc/profile.d/ to make custom changes to your environment, as this
will prevent the need for merging in future updates.
…
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
…
for i in /etc/profile.d/*.sh ; do
if [ -r “$i” ]; then
if [ “KaTeX parse error: Expected '}', got '#' at position 3: {-#̲*i}" != "-” ]; then
. “$i”
else
. “$i” >/dev/null 2>&1
fi
fi
done
unset i
unset -f pathmunge
[root@CentOS6 ~]#
$HOME启动文件,我的系统用的~/.bash_profile,这些文件都是以.开头,代表了都是隐藏文件,同时是针对特定用户的,因此用户可以修改该文件。
我们看下~/.bash_profile文件下的内容定义PATH的那一行。 H O M E 文 件 定 义 特 定 用 户 的 P A T H = HOME文件定义特定用户的PATH= HOME文件定义特定用户的PATH=PATH: H O M E / b i n , 代 表 我 们 可 以 将 可 执 行 文 件 放 在 HOME/bin,代表我们可以将可执行文件放在 HOME/bin,代表我们可以将可执行文件放在HOME/bin目录下。
[root@CentOS6 profile.d]# cat ~/.bash_profile
.bash_profile
Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
User specific environment and startup programs
PATH= P A T H : PATH: PATH:HOME/bin
export PATH
6.2 非登录式shell
登录式shell是需要输入用户名、密码登录的shell,而非登录式shell则是不需要的,例如直接在命令行输入bash、在图形化界面点击open in terminal开启命令行终端等都是非登录式shell。
另外,对于退出shell的命令exit和logout的区别,exit命令可以退出登录式shell和非登录式shell,logout只能退出登录式shell。
da82c85c84971a40125d6ceb5742b2aa.png
我们可以通过$0变量值来查看是登录式shell还是非登录式shell,登录式shell会在前面显示‘-’非登录式shell则没有
69f2e5b7718355cb8ce20514dd57a0fa.png
在非登录式shell的启动过程中,由于不需要重复的登录shell,所以非登录shell只需要执行下列文件即可,
$HOME/.bashrc # 下面的内容说明
[root@CentOS6 ~]# cat ~/.bashrc
.bashrc
User specific aliases and functions
alias rm=‘rm -i’
alias cp=‘cp -i’
alias mv=‘mv -i’
alias cdnet=‘cd /etc/sysconfig/network-scripts/’
alias ping=‘ping -c 4’
Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
该$HOME/.bashrc可以定义用户自定义的别名和函数,另外还有引用公共/etc/bashrc下的变量,我们来看看/etc/bashrc文件内容[root@CentOS6 ~]# cat /etc/bashrc
/etc/bashrc
System wide functions and aliases
Environment stuff goes in /etc/profile
It’s NOT a good idea to change this file unless you know what you
are doing. It’s much better to create a custom.sh shell script in
/etc/profile.d/ to make custom changes to your environment, as this
will prevent the need for merging in future updates.
are we an interactive shell?
…
Only display echos from profile.d scripts if we are no login shell
and interactive - otherwise just process them to set envvars
for i in /etc/profile.d/*.sh; do
if [ -r “$i” ]; then
if [ “$PS1” ]; then
. “$i”
else
. “$i” >/dev/null 2>&1
fi
fi
done
unset i
unset pathmunge
fi
vim:ts=4:sw=4
另外该文件也会执行/etc/profile.d/*.sh来设定特定应用程序的环境变量。
其实登录式shell也会执行 H O M E / . b a s h r c , 可 以 回 到 上 面 的 / . b a s h p r o f i l e 的 代 码 部 分 , 我 们 会 发 现 该 文 件 中 会 调 用 HOME/.bashrc,可以回到上面的~/.bash_profile的代码部分,我们会发现该文件中会调用 HOME/.bashrc,可以回到上面的 /.bashprofile的代码部分,我们会发现该文件中会调用HOME/.bashrc文件。这样说可以加深登录式shell和非登录式shell的本质区别。
6.3 交互式shell
我们通过终端登录Linux,输入命令,shell执行命令并实时返回结果,退出。这种模式就是交互式shell。
在交互式shell下,bash不会执行/etc/profile文件,代替而之的是$HOME/.bashrc文件,执行的启动文件和非登录式shell一样。
这个文件定义新交互式shell的环境变量,该文件最好不要定义全局环境变量(export),另外该文件也会执行/etc/profile.d/*.sh来设定特定应用程序的环境变量。任何开启交互式子shell(bash、su - user)的操作都会读取$HOME/.bashrc。
6.4 非交互式shell
和交互式shell相反,该模式下shell不与终端进行交互,例如以shell脚本的方式读取脚本中命令,而不需要与终端交互(除非需要用户输入参数的命令),当文件结束时,该shell也就退出了。
非交互式shell的相关启动文件和系统设置的一个全局环境变量BASH_ENV相关。该变量默认情况下没有定义。我们需要手动设置该变量,当执行shell脚本的时候,会执行该变量指向的文件。
1da03d7635d0b507dc291abc5f8fd1db.png
ce1819ca3457e9b4a761dfebfa65a90f.png
f6f1e0c1c1b08b9418295bfe3d3ec2c5.png
我们可以利用$-的变量值来查看当前shell是交互式还是非交互式的,如下图:
vim tmp.sh
#!/bin/bash
echo $-
7fb67cf906d1f6ee5a13a3a16a806480.png
区分交互式和非交互式就是看是否有‘i’(interactive),可以看出脚本中为非交互式,我们平时用的终端为交互式。
6.5 总结
登录式shell,包括依次要执行的启动文件和文件代码部分要调用的文件,对他们概括如下:
a4ee75cb04a44842c553b1ff2b176d7e.png
非登录式shell
57697d497617c624688d5cd2b3224c4f.png
交互式shell
执行启动文件过程类似于非登录式shell
非交互式shell
执行BASH_ENV全局环境变量指向的文件
知道了这些启动文件的区别后,我们可以针对性的修改启动文件以使自定义的全局环境变量、别名等永久生效,例如我们可以将所有自定义的全局环境变量放在一个.sh结尾的文件中,然后将该文件放到/etc/profile.d/目录下或者将自定义的变量放入/etc/bashrc文件中,这样将对所有的用户都生效。而对于一些针对个人用户的别名等,可以将其写入到~/.bashrc文件中,只对单个用户有效。
####################################@@@@@@@@@@@@@@@
http://www.360doc.com/content/20/1218/17/15915859_952230669.shtml