linux命令(56):環境變量:/etc/profile、/etc/bashrc 、~/.profile、~/.bashrc

添加環境變量:http://www.javashuo.com/article/p-ynsbmyob-cv.htmlhtml

1、環境變量介紹:
 在Linux系統中,環境變量按照其做用範圍不一樣大體能夠分爲系統級環境變量和用戶級環境變量。linux

  • 系統級環境變量:每個登陸到系統的用戶都可以讀取到系統級的環境變量;
  • 用戶級環境變量:每個登陸到系統的用戶只可以讀取屬於本身的用戶級的環境變量;

  天然而然地,環境變量的配置文件也相應的被分紅了系統級和用戶級兩種。shell

2、系統級:ubuntu

一、/etc/profile
在系統啓動後第一個用戶登陸時運行,並從/etc/profile.d目錄的配置文件中搜集shell的設置,使用該文件配置的環境變量將應用於登陸到系統的每個用戶。bash

提示:在Linux系統中,使用如下命令可使配置文件馬上生效。session

  source /etc/profile
  echo $PATH

 

二、/etc/bashrc(Ubuntu和Debian中是/etc/bash.bashrc)
在 bash shell 打開時運行,修改該文件配置的環境變量將會影響全部用戶使用的bash shell。spa

 

注意:這裏的bash shell有不一樣的類別,不一樣的類別所使用的環境變量配置文件也有所不一樣。翻譯

通常狀況下,非登陸shell不會執行任何profile文件,非交互shell模式不會執行任何bashrc文件。code

 

三、/etc/environment
在系統啓動時運行,用於配置與系統運行相關但與用戶無關的環境變量,修改該文件配置的環境變量將影響全局。htm

 

3、用戶級:

一、~/.profile(推薦首選)
~/.profile: executed by Bourne-compatible login shells.

當用戶登陸時執行,每一個用戶均可以使用該文件來配置專屬於本身使用的shell信息。

二、~/.bashrc
~/.bashrc: executed by bash(1) for non-login shells.

當用戶登陸時以及每次打開新的shell時該文件都將被讀取,不推薦在這裏配置用戶專用的環境變量,由於每開一個shell,該文件都會被讀取一次,效率確定受影響。
生效用:source
bashrc和profile的差別
從上面的英文描述能夠知道,bashrc和profile的差別在於:

  注意:一般咱們修改bashrc,有些linux的發行版本不必定有profile這個文件;

    • 1. bashrc 是在系統啓動後就會自動運行。
    • 2. profile 是在用戶登陸後纔會運行。
    • 3. 進行設置後,可運用source bashrc命令更新bashrc,也可運用source profile命令更新profile。
    • 4. /etc/profile中設定的變量(全局)的能夠做用於任何用戶,而~/.bashrc等中設定的變量(局部)只能繼承/etc/profile中的變量,他們是"父子"關係。

三、~/.bash_profile 或 ~./bash_login

~/.bash_profile or ~./bash_login - If one of these file exist, bash executes it rather then "~/.profile"
when it is started as a login shell. (Bash will prefer "~/.bash_profile" to "~/.bash_login").
However, these files won't influence a graphical session by default.

以上是ubuntu官網給出的關於~/.bash_profile 和 ~./bash_login 的說明,翻譯爲中文:
~/.bash_profile 或 ~./bash_login -
若是有其中的一個文件存在的話, 當啓動的是一個登陸shell時,Bash 會執行該文件而不會執行~/.profile ;
若是兩個文件都存在的話,Bash 將會優先執行~/.bash_profile 而不是~/.bash_login ;
然而, 默認狀況下,這些文件不會影響圖形會話。


四、~/.bash_logout
當每次退出系統(退出bash shell)時執行該文件。

注意:Linux系統使用 $VARIABLE_NAME 訪問環境變量,多個環境變量之間使用 「:」分隔,Windows系統使用 %VARIABLE_NAME% 訪問環境變量,多個環境變量之間使用 ; 分隔。

4、執行順序

通常狀況下,Linux加載環境變量配置文件的執行順序爲:

==> /etc/profile
==> ~/.bash_profile | ~/.bash_login | ~/.profile
==> ~/.bashrc
==> /etc/bashrc
==> ~/.bash_logout
相關文章
相關標籤/搜索