我们只要使用linux shell,就避免不了环境变量这个话题,大家最开始接触环境变量估计都从接触PATH环境变量开始。
环境变量按照生效范围可以分为全局环境变量和局部环境变量。全局环境变量对于shell会话和所有生成的子shell都是可见的;局部环境变量只对当前shell有效。Linux系统在你开始bash会话时就会设置一些全局环境变量,而且变量名都是大写,以区别于普通的环境变量。
使用printenv或env可以查看这些默认的全局环境变量。
[root@aliyun-hk1 profile.d]# printenvXDG_SESSION_ID=1541HOSTNAME=aliyun-hk1TERM=cygwinSHELL=/bin/bashHISTSIZE=1000SSH_CLIENT=222.90.142.215 54197 22SSH_TTY=/dev/pts/0USER=rootLS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:MAIL=/var/spool/mail/rootPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binPWD=/etc/profile.dLANG=en_US.UTF-8HISTCONTROL=ignoredupsSHLVL=1HOME=/rootLOGNAME=rootSSH_CONNECTION=222.90.142.215 54197 172.31.200.164 22LESSOPEN=||/usr/bin/lesspipe.sh %sXDG_RUNTIME_DIR=/run/user/0_=/usr/bin/printenvOLDPWD=/root
显示单个全局变量的值,例如:HOME、PATH
[root@aliyun-hk1 profile.d]# env|grep HOMEHOME=/root[root@aliyun-hk1 profile.d]# env|grep PATHPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin[root@aliyun-hk1 profile.d]# echo $HOME/root[root@aliyun-hk1 profile.d]# echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin[root@aliyun-hk1 profile.d]#
全局环境变量一般在以下几处定义或配置, 从优先级的高低分为/etc/profile、/etc/bashrc、/etc/profile.d/
[root@aliyun-hk1 profile.d]# 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.pathmunge () {case ":${PATH}:" in*:"$1":*);;*)if [ "$2" = "after" ] ; thenPATH=$PATH:$1elsePATH=$1:$PATHfiesac}...[root@aliyun-hk1 profile.d]# 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.[root@aliyun-hk1 profile.d]# cd /etc/profile.d/[root@aliyun-hk1 profile.d]# lltotal 64-rw-r--r-- 1 root root 771 Sep 27 2018 256term.csh-rw-r--r-- 1 root root 841 Sep 27 2018 256term.sh-rw-r--r--. 1 root root 196 Mar 25 2017 colorgrep.csh-rw-r--r--. 1 root root 201 Mar 25 2017 colorgrep.sh-rw-r--r--. 1 root root 1741 Apr 11 2018 colorls.csh-rw-r--r--. 1 root root 1606 Apr 11 2018 colorls.sh-rw-r--r--. 1 root root 80 Apr 11 2018 csh.local-rw-r--r-- 1 root root 1706 Sep 27 2018 lang.csh-rw-r--r-- 1 root root 2703 Sep 27 2018 lang.sh-rw-r--r--. 1 root root 123 Jul 31 2015 less.csh-rw-r--r--. 1 root root 121 Jul 31 2015 less.sh-rw-r--r--. 1 root root 81 Apr 11 2018 sh.local-rw-r--r-- 1 root root 105 Apr 11 2018 vim.csh-rw-r--r-- 1 root root 269 Apr 11 2018 vim.sh-rw-r--r--. 1 root root 164 Jan 28 2014 which2.csh-rw-r--r--. 1 root root 169 Jan 28 2014 which2.sh
局部环境变量可以分为用户级环境变量和进程级环境变量。用户级环境变量在当前用户的任何shell都有效,进程级环境变量只在运行进程的shell中有效。
用户级环境变量在这个用户shell启动的时候会自动加载,按照顺序运行第一个被找到的文件,后面的直接忽略。这些文件都在当前用户的家目录下以隐藏文件的形式存在,用户可以根据实际需要进行增删改。如果修改配置文件后必须重新登陆该用户或者执行source filename 或者 . filename才会生效。
~/.bash_profile 1~/.bashrc~/.bash_login 2~/.profile 3
进程级环境变量在运行这个进程时临时定义,只在当前进程的shell及子shell中有效。当然,每个进程的shell及子shell也会继承全局环境变量。
[root@aliyun-hk1 ~]# bash[root@aliyun-hk1 ~]# name=robin[root@aliyun-hk1 ~]# echo $namerobin[root@aliyun-hk1 ~]# ps -fUID PID PPID C STIME TTY TIME CMDroot 3142 3140 0 00:21 pts/0 00:00:00 -bashroot 3189 3142 0 00:22 pts/0 00:00:00 bashroot 3216 3189 0 00:22 pts/0 00:00:00 ps -f[root@aliyun-hk1 ~]# exitexit[root@aliyun-hk1 ~]# echo $name[root@aliyun-hk1 ~]#
文章转载自运维时刻,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




