环境变量——bash shell使用环境变量来存储系统相关数据,并容许将数据存储在内存中。linux
环境变量分为:全局环境变量
shell
本地环境变量vim
目录bash
全局环境变量less
本地环境变量ssh
设置全局环境变量ide
删除环境变量函数
PATH全局环境变量this
设置系统环境变量的相关文件(登陆、非登陆、交互、非交互shell)idea
1、全局环境变量
在当前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时设置的
2、本地环境变量
只在当前shell中可见
能够经过set命令查看,不过set命令查看的是全部环境变量(全局和本地)
注意在设置环境变量的时候,[变量=值]之间不能添加空格,要否则shell会把它当作一个单独的命令执行
3、设置全局环境变量
使用export命令将本地环境变量变为全局环境变量
4、删除环境变量
使用unset命令能够删除环境变量,格式为
unset 变量名
不过对于全局环境变量的删除,咱们要注意:
若是在子shell下删除全局环境变量,删除操做只对子shell有效,若是回到父shell下,该全局变量还能引用
5、PATH全局环境变量
修改PATH环境变量:
PATH=$PATH:新加目录
小技巧:
咱们能够将PATH设置为PATH=$PATH:.(单个点表明当前工做目录)
6、设置系统环境变量的相关文件
系统环境变量是在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 [ "${-#*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的那一行。$HOME文件定义特定用户的PATH=$PATH:$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=$PATH:$HOME/bin export PATH
6.2 非登陆式shell
登陆式shell是须要输入用户名、密码登陆的shell,而非登陆式shell则是不须要的,例如直接在命令行输入bash、在图形化界面点击open in terminal开启命令行终端等都是非登陆式shell。
另外,对于退出shell的命令exit和logout的区别,exit命令能够退出登陆式shell和非登陆式shell,logout只能退出登陆式shell。
咱们能够经过$0变量值来查看是登陆式shell仍是非登陆式shell,登陆式shell会在前面显示‘-’非登陆式shell则没有
在非登陆式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也会执行$HOME/.bashrc,能够回到上面的~/.bash_profile的代码部分,咱们会发现该文件中会调用$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脚本的时候,会执行该变量指向的文件。
咱们能够利用$-的变量值来查看当前shell是交互式仍是非交互式的,以下图:
vim tmp.sh #!/bin/bash echo $-
区分交互式和非交互式就是看是否有‘i’(interactive),能够看出脚本中为非交互式,咱们平时用的终端为交互式。
6.5 总结
登陆式shell,包括依次要执行的启动文件和文件代码部分要调用的文件,对他们归纳以下:
非登陆式shell
交互式shell
执行启动文件过程相似于非登陆式shell
非交互式shell
执行BASH_ENV全局环境变量指向的文件
知道了这些启动文件的区别后,咱们能够针对性的修改启动文件以使自定义的全局环境变量、别名等永久生效,例如咱们能够将全部自定义的全局环境变量放在一个.sh结尾的文件中,而后将该文件放到/etc/profile.d/目录下或者将自定义的变量放入/etc/bashrc文件中,这样将对全部的用户都生效。而对于一些针对我的用户的别名等,能够将其写入到~/.bashrc文件中,只对单个用户有效。