在自定義安裝軟件的時候,常常須要配置環境變量,下面列舉出各類對環境變量的配置方法。html
下面全部例子的環境說明以下:mysql
讀取環境變量的方法:sql
這兩個命令執行的效果以下shell
uusama@ubuntu:~$ export declare -x HOME="/home/uusama" declare -x LANG="en_US.UTF-8" declare -x LANGUAGE="en_US:" declare -x LESSCLOSE="/usr/bin/lesspipe %s %s" declare -x LESSOPEN="| /usr/bin/lesspipe %s" declare -x LOGNAME="uusama" declare -x MAIL="/var/mail/uusama" declare -x PATH="/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" declare -x SSH_TTY="/dev/pts/0" declare -x TERM="xterm" declare -x USER="uusama" uusama@ubuntu:~$ echo $PATH /home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
其中PATH變量定義了運行命令的查找路徑,以冒號:分割不一樣的路徑,使用export定義的時候可加雙引號也可不加。ubuntu
使用export命令直接修改PATH的值,配置MySQL進入環境變量的方法:vim
export PATH=/home/uusama/mysql/bin:$PATH #或者把PATH放在前面 export PATH=$PATH:/home/uusama/mysql/bin
注意事項:bash
經過修改用戶目錄下的~/.bashrc文件進行配置:app
vim ~/.bashrc # 在最後一行加上 export PATH=$PATH:/home/uusama/mysql/bin
注意事項:less
和修改~/.bashrc文件相似,也是要在文件最後加上新的路徑便可:ide
vim ~/.bash_profile # 在最後一行加上 export PATH=$PATH:/home/uusama/mysql/bin
注意事項:
該方法是修改系統配置,須要管理員權限(如root)或者對該文件的寫入權限:
# 若是/etc/bashrc文件不可編輯,須要修改成可編輯 chmod -v u+w /etc/bashrc vim /etc/bashrc # 在最後一行加上 export PATH=$PATH:/home/uusama/mysql/bin
注意事項:
該方法修改系統配置,須要管理員權限或者對該文件的寫入權限,和vim /etc/bashrc相似:
# 若是/etc/profile文件不可編輯,須要修改成可編輯 chmod -v u+w /etc/profile vim /etc/profile # 在最後一行加上 export PATH=$PATH:/home/uusama/mysql/bin
注意事項:
該方法是修改系統環境配置文件,須要管理員權限或者對該文件的寫入權限:
# 若是/etc/bashrc文件不可編輯,須要修改成可編輯 chmod -v u+w /etc/environment vim /etc/profile # 在最後一行加上 export PATH=$PATH:/home/uusama/mysql/bin
注意事項:
上面列出了環境變量的各類配置方法,那麼Linux是如何加載這些配置的呢?是以什麼樣的順序加載的呢?
特定的加載順序會致使相同名稱的環境變量定義被覆蓋或者不生效。
環境變量的分類
環境變量能夠簡單的分紅用戶自定義的環境變量以及系統級別的環境變量。
另外在用戶環境變量中,系統會首先讀取~/.bash_profile(或者~/.profile)文件,若是沒有該文件則讀取~/.bash_login,根據這些文件中內容再去讀取~/.bashrc。
爲了測試各個不一樣文件的環境變量加載順序,咱們在每一個環境變量定義文件中的第一行都定義相同的環境變量UU_ORDER,該變量的值爲自己的值鏈接上當前文件名稱。
須要修改的文件以下:
/etc/environment /etc/profile /etc/profile.d/test.sh,新建文件,沒有文件夾可略過 /etc/bashrc,或者/etc/bash.bashrc ~/.bash_profile,或者~/.profile ~/.bashrc
在每一個文件中的第一行都加上下面這句代碼,並相應的把冒號後的內容修改成當前文件的絕對文件名。
export UU_ORDER="$UU_ORDER:~/.bash_profile"
修改完以後保存,新開一個窗口,而後echo $UU_ORDER觀察變量的值:
uusama@ubuntu:~$ echo $UU_ORDER $UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc
能夠推測出Linux加載環境變量的順序以下:
/etc/environment /etc/profile /etc/bash.bashrc /etc/profile.d/test.sh ~/.profile ~/.bashrc
由上面的測試可容易得出Linux加載環境變量的順序以下,:
系統環境變量 -> 用戶自定義環境變量 /etc/environment -> /etc/profile -> ~/.profile
打開/etc/profile文件你會發現,該文件的代碼中會加載/etc/bash.bashrc文件,而後檢查/etc/profile.d/目錄下的.sh文件並加載。
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1)) # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). if [ "$PS1" ]; then if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then # The file bash.bashrc already sets the default PS1. # PS1='h:w$ ' if [ -f /etc/bash.bashrc ]; then . /etc/bash.bashrc fi else if [ "`id -u`" -eq 0 ]; then PS1='# ' else PS1='$ ' fi fi fi if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset i fi
其次再打開~/.profile文件,會發現該文件中加載了~/.bashrc文件。
# if running bash if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi # set PATH so it includes user's private bin directories PATH="$HOME/bin:$HOME/.local/bin:$PATH"
從~/.profile文件中代碼不難發現,/.profile文件只在用戶登陸的時候讀取一次,而/.bashrc會在每次運行Shell腳本的時候讀取一次。
能夠自定義一個環境變量文件,好比在某個項目下定義uusama.profile,在這個文件中使用export定義一系列變量,而後在~/.profile文件後面加上:sourc uusama.profile,這樣你每次登錄均可以在Shell腳本中使用本身定義的一系列變量。
也能夠使用alias命令定義一些命令的別名,好比alias rm="rm -i"(雙引號必須),並把這個代碼加入到~/.profile中,這樣你每次使用rm命令的時候,都至關於使用rm -i命令,很是方便。
來源:https://www.cnblogs.com/youyo...