linux環境變量

linux環境變量

定義

(1)login shell:取得bash 時須要完整的登入流程,就稱爲login shell。舉例來講,同tty1~tty6登入時,須要輸入用戶名和密碼,此時取得的bash就稱爲login shellhtml

(2)non-login shell:取得bash介面的方法不須要重複登入的動做。linux

舉兩個例子
(i)以X window登入linux後,再以X的圖形化介面啓動終端機,此時那個終端機並不須要再次的輸入用戶名和密碼,那個bash的環境就稱爲non-login shell
(ii)在本來的bashr環境中再次下達bash這個指令,一樣沒有要求輸入用戶名和密碼,那個第二個bash也是non-login shellweb

Login Shell Sessions&&Nonlogin Shell Session

Login Shell Sessions:shell

  • /etc/profile : a global configuration script that applies to all users.
  • ~/.bash_profile : a user’s personal startup file.can be used to extend or overide settings in the global configuration script.
  • ~/.bash_login : if ~/.bash_profile is not found,bash attempts to read this script.
  • ~/.profile : if neither ~/.bash_profile nor ~/.bash_login is found,bash attempts to read this file. This is the default in Debian-based distributions,such as Ubuntu.

Nonlogin Shell Sessions:bash

  • /etc/bash.bashrc a global configuration script that applies to all users.
  • ~/.bashrc a user’s personal startup file. Can be used to extend or override settings in the global configuration script.

.bashrc與.bash_profile區別

  • .bashrc登陸以及打開新的shell時,該文件被讀取。
  • .bash_profile默認狀況之下,它用來設置一些環境變量,執行用戶的.bashrc文件。
  • 執行順序分爲:.bash_profile 、bash_login、profile,而且在~/.bash_profile中通常含有如下代碼:
  1. if [ -f ~/.bashrc ] ; then 
  2. . ~/.bashrc 
  3. fi 

~/.bashrc中還會有如下代碼:session

  1. if [ -f /etc/bashrc ] ; then 
  2. . /bashrc 
  3. fi 

因此,~/.bashrc會調用 /etc/bashrc文件。最後,在退出shell時,還會執行 ~/.bash_logout文件。
執行順序爲:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout
注:/etc/profile: 此文件爲系統的每一個用戶設置環境信息,當用戶第一次登陸時,該文件被執行. 並從/etc/profile.d目錄的配置文件中搜集shell的設置。app