微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CentOS中环境变量和配置文件

什么是环境变量

bash shell用一个叫做 环境变量(environment variable) 的特性来存储有关shell会话和工作环境的信息。即允许在内存中存储数据,使得在程序或shell中运行的脚本能够访问它们。

在bash shell中,环境变量分为两类:

全局环境变量

全局环境变量对于shell会话和所有生成的子shell都是可见的。局部变量则只对创建它们的shell可见。

查看全局变量,可以使用envprintenv命令。

[[email protected] ~]# env
HOSTNAME=localhost
TERM=linux
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=10.0.100.17 56344 22
SSH_TTY=/dev/pts/0
USER=root


[[email protected] ~]# 
[[email protected] ~]# printenv
HOSTNAME=localhost
TERM=linux
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=10.0.100.17 56344 22
SSH_TTY=/dev/pts/0
USER=root


[[email protected] ~]# printenv TERM
linux

使用环境变量,通过 $ +变量名。

[[email protected] ~]# echo $HOME
/root

系统环境变量基本上都是使用大写字母,以区别于普通用户的环境变量。

局部环境变量

顾名思义,局部环境变量只能在定义它们的进程中可见。set命令会显示某个特定进程设置的所有环境变量,包括局部变量、全局变量以及用户定义变量。

[[email protected] ~]# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_Cmds=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.1.2(1)-release'
COLORS=/etc/DIR_COLORS
COLUMNS=165

用户定义变量

一旦启动了bash shell,就能创建在这个shell进程内可见的局部变量。该进程创建的子shell无法读取父shell的局部变量。

[[email protected] shell]# sh a.sh

2
22
2
[[email protected] shell]# cat a.sh
#!/bin/bash

a=1;
export b=2;

sh b.sh

echo $b;


[[email protected] shell]# cat b.sh
#!/bin/bash

echo $a;
echo $b;
b=22;
echo $b;
[[email protected] shell]# sh a.sh 

2
22
2

用户可以通过export变量,使变量变为全局变量,这样子shell也可以读取到。而子shell修改该变量,父shell中不受影响

如果在子shell中设置环境变量,想要在父shell中读取呢?

一个使用场景是:多个执行脚本依赖于共同的环境配置,这个配置写在一个env.sh脚本里,如何使其他执行脚本可以读取到env.sh里变量?在子shell中export变量,并不能影响到父shell。

source命令(从 C Shell 而来)是bash shell的内置命令。点命令,就是一个点符号,(从Bourne Shell而来)是source的另一名称。这两个命令都以一个脚本为参数,该脚本将作为当前shell的环境执行,即不会启动一个新的子进程。所有在脚本中设置的变量将成为当前Shell的一部分

[[email protected] shell]# cat c.sh
. ./env.sh
source ./profile.sh

echo $env;
echo $profile;
[[email protected] shell]# cat env.sh
env='test';
[[email protected] shell]# cat profile.sh 
profile="dev";
[[email protected] shell]# sh c.sh 
test
dev

如果想要删除环境变量

unset var_name

设置全局环境变量

上文中,可以知道,如果想要在本进程和子进程中使用共同的环境变量。通过source命令去读取同一个环境变量脚本可以实现。这是用户自定义的方案。但很多时候,我们需要读取的全局环境变量并不知道source,所以需要一个认的环境变量读取文件

当你登录Linux系统时,bash shell会作为登录shell启动。登录shell会从5个不同的启动文件里读取

  • /etc/profile
  • $HOME/.bash_profile
  • $HOME/.bashrc
  • $HOME/.bash_login
  • $HOME/.profile

/etc/profile

/etc/profile文件是bash shell认的主启动文件。只要你登录了Linux系统,bash就会执行/etc/profile启动文件的命令。

[[email protected] shell]# 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" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`id -u`
        UID=`id -ru`
    fi
    USER="`id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
    pathmunge /sbin after
fi

HOSTNAME=`/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default,we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You Could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi

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

文件会读取/etc/profile.d/下所有的*.sh文件,通过点命令(source)来加载变量。即在/etc/profile和/etc/profile.d/*.sh定义的变量,都是全局的系统环境变量。

$HOME/.bash_profile

$HOME下的启动文件都是用户专属的启动文件,定义该用户的环境变量。而/etc/profile则是系统的,所有用户的环境变量。

shell会按照下列顺序,运行第一个找到的文件,余下被忽略:

  • $HOME/.bash_profile
  • $HOME/.bash_login
  • $HOME/.profile

.bashrc通过.bash_profile来调用

[[email protected] shell]# 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

总结

将要设置的系统全局环境变量,比如JAVA_HOME,放在/etc/profile.d/目录下,以*.sh脚本的形式定义。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。