環境變數 env

  1. Linux 是一個多人多功的系統,每個用戶登入時讀取設定檔 /etc/bashrc 及 /etc/profile 取得工作的環境變數,登入後執行 env 可以列出環境變數。
    [dywang@dywmac ~]$ env
    ORBIT_SOCKETDIR=/tmp/orbit-dywang
    HOSTNAME=dywmac
    IMSETTINGS_INTEGRATE_DESKTOP=yes
    TERM=xterm
    SHELL=/bin/bash
    HISTSIZE=1000
    XDG_SESSION_COOKIE=ac14e52ecfb2492f78ca6a6900000012-1552852991.392630-81397834
    GTK_RC_FILES=/etc/gtk/gtkrc:/home/dywang/.gtkrc-1.2-gnome2
    WINDOWID=69206020
    QTDIR=/usr/lib64/qt-3.3
    QTINC=/usr/lib64/qt-3.3/include
    IMSETTINGS_MODULE=gcin
    USER=dywang
    LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;....
    GNOME_KEYRING_SOCKET=/tmp/keyring-8bgO8w/socket
    SSH_AUTH_SOCK=/tmp/keyring-8bgO8w/socket.ssh
    USERNAME=dywang
    SESSION_MANAGER=local/unix:@/tmp/.ICE-unix/3513,unix/unix:/tmp/.ICE-unix/3513
    PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
    MAIL=/var/spool/mail/dywang
    DESKTOP_SESSION=gnome
    QT_IM_MODULE=gcin
    PWD=/home/dywang
    XMODIFIERS=@im=gcin
    GDM_KEYBOARD_LAYOUT=us
    GNOME_KEYRING_PID=3503
    LANG=en_US.UTF-8
    GDM_LANG=en_US.UTF-8
    GDMSESSION=gnome
    SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
    HISTCONTROL=ignoredups
    HOME=/home/dywang
    SHLVL=2
    GNOME_DESKTOP_SESSION_ID=this-is-deprecated
    LOGNAME=dywang
    QTLIB=/usr/lib64/qt-3.3/lib
    CVS_RSH=ssh
    DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-CSMOPauJQO,guid=51ed8f7a36376c036368676d0000002e
    LESSOPEN=||/usr/bin/lesspipe.sh %s
    WINDOWPATH=1
    DISPLAY=:0.0
    G_BROKEN_FILENAMES=1
    COLORTERM=gnome-terminal
    XAUTHORITY=/var/run/gdm/auth-for-dywang-xnorl4/database
    _=/usr/bin/env
    
  2. 除了系統預設的環境變數,用戶也可以在自己的家目錄中 .bashrc.bash_profile 設定專屬的環境變數。 變數設定在 /etc/bashrc 或 /etc/profile 兩個檔案 ssh 登入都會生效,但圖形界面開啟 gnome-terminal 不會讀取 /etc/profile,同樣的狀況,用戶設定在家目錄中的 .bashrc 或 .bash_profile ,ssh 登入都生效,但 gnome-terminal 不會讀取 .bash_profile
    [dywang@dywmac ~]$ vim .bashrc 
    [dywang@dywmac ~]$ tail -1 .bashrc
    myname=dywang
    [dywang@dywmac ~]$ vim .bash_profile 
    [dywang@dywmac ~]$ tail -1 .bash_profile 
    doc=Documents
    [dywang@dywmac ~]$ bash
    [dywang@dywmac ~]$ echo $myname
    dywang
    [dywang@dywmac ~]$ echo $doc
    
    [dywang@dywmac ~]$ exit
    exit
    [dywang@dywmac ~]$ ssh localhost
    Last login: Mon Mar 18 04:56:47 2019 from localhost
    [dywang@dywmac ~]$ echo $myname
    dywang
    [dywang@dywmac ~]$ echo $doc
    Documents