環境變數設定檔主要分兩類
全體使用者環境變數設定:(只有 root 可以修改)
個人環境變數設定:(開放使用者可自行修改)
全體使用者環境變數設定檔位於 /etc/profile ; 請 「cat /etc/profile」 簡單閱讀一下內容。 /etc/profile 只有 root 可以修改,請 「ls -l /etc/profile」 看一下該檔案的所有權人以及相關檔案屬性。 使用者登入後使用 BASH 的同時,第一時間會來執行 /etc/profile 這個檔案,然後才是 ~/.bash_profile, ~/.bash_login , ~/.profile(細節請 man bash)。 系統管理者可透過撰寫 /etc/profile 來提供使用者一個初始化的使用者環境設定, 這件事情是系統管理者必須做到的義務(提供一簡單的使用者環境設定)。以下是筆者提供一簡單的設定,請依照自己的需求 適度的修改。
$ edit /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
PATH="/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games"
if [ "$BASH" ]; then
PS1='\u@\h:\w\$ '
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
export PATH PS1
umask 022
#============================= 以下開始 ==================================
export EDITOR=jed # 設定預設 editor 為 jed ,不設定預設為 vi
mesg y # 開啟別人可對你發送線上訊息,如不願意被人打擾請改為 mesg n
export PS1='\T \u@\h:\w $ ' # 設定你的提示符號
alias ls="ls --color" # alias 設定
alias dir="ls --color"
alias wdir="dir | more"
alias type="cat"
alias rm="rm -i"
export PATH="/usr/local/script:$PATH" # 增加 /usr/local/script 於可執行檔尋找範圍
|
個人環境變數設定,顧名思義這是使用者自行更改環境變數的方法,系統管理者無須偏勞。 習慣上每一個使用者的家目錄都不會一樣,習慣上都應該位於 /home 下面,以 test 這個使用者為例, test 的家目錄習慣上應該位於 /home/test 。每一個使用者家目錄下應該都會有個 隱藏的檔案 ~/.bashrc,請 「ls -la ~/」。而這個檔案就是個人的「個人環境變數設定檔」, 設定作法同「全體使用者環境變數設定檔」 一樣都是直接編輯該檔案。由於 「/etc/profile」 會最先執行,而才是個人的「~/.bashrc」, 如果 「/etc/profile」設定了 PS1 ,而 「~/.bashrc」又設定了 PS1 ,聰明如你你認為應該是什麼樣的情形呢? 以下是筆者提供一簡單的設定,請依照自己的需求適度的修改。
$ edit ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files for examples
# If running interactively, then:
if [ "$PS1" ]; then
# enable color support of ls and also add handy aliases
eval `dircolors`
alias ls='ls --color=auto '
alias ll='ls -l'
alias la='ls -A'
alias l='ls -CF'
alias dir='ls --color=auto --format=vertical'
alias vdir='ls --color=auto --format=long'
# set a fancy prompt
PS1='\u@\h:\w\$ '
fi
|
![]() | 隱藏的檔案或目錄 |
|---|---|
凡是以 . 為開頭的檔案或目錄,在 Linux 上都稱呼它們為隱藏的檔案或目錄,通常都是程式的設定檔,平常使用 ls 如不特別指定 -a 這個參數的話,應該都不會看的到這些檔案。而這些檔案通常都會以 rc 作為檔名的結尾。 |
![]() | /etc/profile 與 ~/.bashrc ,PS1 的設定 | |
|---|---|---|
我們舉這樣的例子說明:
|
關於環境變數設定的細節部份可查閱 man bash 尋找 profile 這個關鍵字串。你將發現以下這段話, 說明執行的優先順序。細心的人請注意 login shell 於 nologin shell 的分別。
When bash is invoked as an interactive login shell, or as
a non-interactive shell with the --login option, it first
reads and executes commands from the file /etc/profile, if
that file exists. After reading that file, it looks for
~/.bash_profile, ~/.bash_login, and ~/.profile, in that
order, and reads and executes commands from the first one
that exists and is readable. The --noprofile option may
be used when the shell is started to inhibit this behav-
ior. |