CentOS中環境變數和設定檔

來源:互聯網
上載者:User

標籤:around   oca   特性   wan   調用   環境配置   全域變數   tar   x86   

什麼是環境變數

bash shell用一個叫做 環境變數(environment variable) 的特性來儲存有關shell會話和工作環境的資訊。即允許在記憶體中儲存資料,使得在程式或shell中啟動並執行指令碼能夠訪問它們。

在bash shell中,環境變數分為兩類:

  • 全域變數
  • 局部變數
全域環境變數

全域環境變數對於shell會話和所有產生的子shell都是可見的。局部變數則只對建立它們的shell可見。

查看全域變數,可以使用envprintenv命令。

[[email protected] ~]# envHOSTNAME=localhostTERM=linuxSHELL=/bin/bashHISTSIZE=1000SSH_CLIENT=10.0.100.17 56344 22SSH_TTY=/dev/pts/0USER=root[[email protected] ~]# [[email protected] ~]# printenvHOSTNAME=localhostTERM=linuxSHELL=/bin/bashHISTSIZE=1000SSH_CLIENT=10.0.100.17 56344 22SSH_TTY=/dev/pts/0USER=root[[email protected] ~]# printenv TERMlinux

使用環境變數,通過 $ +變數名。

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

系統內容變數基本上都是使用大寫字母,以區別於普通使用者的環境變數。

局部環境變數

顧名思義,局部環境變數只能在定義它們的進程中可見。set命令會顯示某個特定進程設定的所有環境變數,包括局部變數、全域變數以及使用者定義變數。

[[email protected] ~]# setBASH=/bin/bashBASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepathBASH_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_COLORSCOLUMNS=165
使用者定義變數

一旦啟動了bash shell,就能建立在這個shell進程內可見的局部變數。該進程建立的子shell無法讀取父shell的局部變數。

[[email protected] shell]# sh a.sh2222[[email protected] shell]# cat a.sh#!/bin/basha=1;export b=2;sh b.shecho $b;[[email protected] shell]# cat b.sh#!/bin/bashecho $a;echo $b;b=22;echo $b;[[email protected] shell]# sh a.sh 2222

使用者可以通過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.shsource ./profile.shecho $env;echo $profile;[[email protected] shell]# cat env.shenv='test';[[email protected] shell]# cat profile.sh profile="dev";[[email protected] shell]# sh c.sh testdev

如果想要刪除環境變數

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 manipulationif [ "$EUID" = "0" ]; then    pathmunge /sbin    pathmunge /usr/sbin    pathmunge /usr/local/sbinelse    pathmunge /usr/local/sbin after    pathmunge /usr/sbin after    pathmunge /sbin afterfiHOSTNAME=`/bin/hostname 2>/dev/null`HISTSIZE=1000if [ "$HISTCONTROL" = "ignorespace" ] ; then    export HISTCONTROL=ignorebothelse    export HISTCONTROL=ignoredupsfiexport 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 fileif [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then    umask 002else    umask 022fifor i in /etc/profile.d/*.sh ; do    if [ -r "$i" ]; then        if [ "${-#*i}" != "$-" ]; then            . "$i"        else            . "$i" >/dev/null 2>&1        fi    fidoneunset iunset -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 functionsif [ -f ~/.bashrc ]; then        . ~/.bashrcfi# User specific environment and startup programsPATH=$PATH:$HOME/binexport PATH

總結

將要設定的系統全域環境變數,比如JAVA_HOME,放在/etc/profile.d/目錄下, 以*.sh指令碼的形式定義。

CentOS中環境變數和設定檔

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.