linux中設定環境變數的方法

2021-08-25 05:15:14 字數 2614 閱讀 1928

.bash_profile和.bashrc的什麼區別及啟動過程

linux環境變數設定方法總結(path/ld_library_path)

/etc/profile: 此檔案為系統的每個使用者設定環境資訊,當使用者第一次登入時,該檔案被執行。並從 /etc/profile.d 目錄的配置檔案中蒐集shell的設定。

/etc/bashrc: 為每乙個執行 bash shell 的使用者執行此檔案。當 bash shell 被開啟時,該檔案被讀取。

~/.bash_profile: 每個使用者都可使用該檔案輸入專用於自己使用的 shell 資訊,當使用者登入時,該檔案僅僅執行一次!預設情況下,其他設定的一些環境變數,執行使用者的 .bashrc 檔案。

~/.bashrc: 該檔案包含專用於登陸使用者的 bash shell 的 bash 資訊,當登入時以及每次開啟新的 shell 時,該該檔案被讀取。

~/.bash_logout: 當每次退出 bash shell 時,執行該檔案。

另外,/etc/profile中設定的變數(全域性)的可以作用於任何使用者,而~/.bashrc等中設定的變數(區域性)只能繼承/etc/profile中的變數,他們是」父子」關係。

~/.bash_profile 是互動式、login 方式進入 bash 執行的。

~/.bashrc 是互動式 non-login 方式進入 bash 執行的。

通常二者設定大致相同,前者會呼叫後者。

在登入linux時要執行檔案的過程如下:

在剛登入 linux 時,首先啟動 /etc/profile 檔案,然後再啟動使用者目錄下的 ~/.bash_profile、 ~/.bash_login 或 ~/.profile 檔案中的其中乙個(根據不同的 linux 作業系統的不同,命名不一樣),

執行的順序為:~/.bash_profile、 ~/.bash_login、 ~/.profile。

如果 ~/.bash_profile 檔案存在的話,一般還會執行 ~/.bashrc檔案。

因為在 ~/.bash_profile 檔案中一般會有下面的**:

# get the aliases and functions

if [ -f ~/.bashrc ] ; then

. ./bashrc

fi

~/.bashrc 中,一般還會有以下**:

# source global definitions

if [ -f /etc/bashrc ] ; then

. /bashrc

fi

所以,~/.bashrc 會呼叫 /etc/bashrc 檔案。最後,在退出 shell 時,還會執行 ~/.bash_logout 檔案。

執行順序為:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout

檢視當前環境變數:

echo $path

設定: 

方法一:export path=path:/*** 但是登出後就失效

方法二:修改~/.bashrc或~/.bash_profile或系統級別的/etc/profile

1. 在其中新增例如export path=/opt/activepython-2.7/bin:$path

2. source .bashrc (source命令也稱為「點命令」,也就是乙個點符號(.)。source命令通常用於重新執行剛修改的初始化檔案,使之立即生效,而不必登出並重新登入)

設定:方法一: export  ld_library_path=ld_library_path:/*** 但是登出後就失效

方法二: 修改 ~/.bashrc 或 ~/.bash_profile 或系統級別的 /etc/profile

1. 在其中新增例如 export path=/opt/activep/lib:$ld_library_path     

2. source .bashrc

方法三:這個沒有修改 ld_library_path 但是效果是一樣的實現動態庫的查詢, 

1. /etc/ld.so.conf 下面加一行/usr/local/mysql/lib

2. 儲存過後 ldconfig 一下(ldconfig 命令的用途,主要是在預設搜尋目錄(/lib 和 /usr/lib)以及動態庫配置檔案 /etc/ld.so.conf 內所列的目錄下,搜尋出可共享的動態鏈結庫(格式如前介紹,lib*.so*),進而建立出動態裝入程式(ld.so)所需的連線和快取檔案.快取檔案預設為 /etc/ld.so.cache,此檔案儲存已排好序的動態鏈結庫名字列表。)

方法三設定稍微麻煩,好處是比較不受使用者的限制。

unset path

unset ld_library_path

Linux中設定環境變數的方法

在shell中 顯示變數的命令有三個env,set,export,其中 env 顯示當前使用者的變數 set 顯示當前shell的變數,包括前者 export 顯示當前匯出成為使用者變數的shell變數 zk zhang3k shell變數設定 echo zk 輸出變數 zhang3k set gr...

linux設定環境變數的方法

安裝一些編譯軟體需要配置環境變數,那麼linux下怎麼配置環境變數呢?方法有幾種 例如我的mips linux gcc編譯器在 opt au1200 rm build tools bin 目錄下,build tools就是我的編譯工具,則有如下三種方法來設定環境變數 1 直接用export命令 ex...

linux 環境變數的設定方法

一般來說,配置交叉編譯工具鏈的時候需要指定編譯工具的路徑,此時就需要設定環境變數。例如我的mips linux gcc編譯器在 opt au1200 rm build tools bin 目錄下,build tools就是我的編譯工具,則有如下三種方法來設定環境變數 1 直接用export命令 ex...