Unix的一個重要概念是環(huán)境,被定義的環(huán)境變量。有些系統(tǒng)通過環(huán)境變量,還有一些由shell,或任何程序加載另一個程序。
變量是一個字符串,我們分配一個值。分配的值可以是一個數(shù)字,文本,文件名,移動設(shè)備,或任何其他類型的數(shù)據(jù)。
例如,首先我們設(shè)定一個變量測試,然后我們使用echo命令來訪問它的值:
$TEST="Unix Programming" $echo $TEST Unix Programming
需要注意的是環(huán)境變量設(shè)置,而無需使用$符號,但訪問它們時,我們使用$符號作為前綴。這些變量保持它們的值,直到我們設(shè)計出來shell。
當(dāng)你登錄到系統(tǒng)中,shell經(jīng)過一個階段稱為初始化設(shè)置各種環(huán)境。這通常是一個兩步的過程,涉及的的shell閱讀下列文件:
/etc/profile
profile
過程如下:
shell進(jìn)行檢查,看是否存在文件 /etc/profile文件。
如果它存在,當(dāng)shell讀取。否則,此文件將被跳過。不顯示錯誤消息。
shell檢查,看看是否該文件。配置文件存在于你的home目錄。主目錄是開始在登錄后的目錄
如果它存在,當(dāng)shell讀取它,否則shell跳過。不顯示錯誤消息。
只要這兩個文件被讀取,shell將顯示一個提示:
$
這是提示這里你可以輸入命令,以讓他們執(zhí)行。
注 - 這里詳述shell初始化過程適用于所有的Bourne型shell,但所使用的是bash和ksh一些額外的文件。
文件/etc/profile文件是由UNIX機(jī)器的系統(tǒng)管理員,包含shell初始化所需的信息系統(tǒng)上的所有用戶。
該文件 .profile是在你的控制之下。您可以添加盡可能多的 shell定制信息,只要想這個文件。最小信息集,需要配置包括:
使用的終端的類型
在其中定位命令的目錄的列表
列表變量,影響終端的外觀和風(fēng)格。
可以檢查 .profile,在home目錄。使用vi編輯器打開它,并檢查所有的變量設(shè)置環(huán)境。
通常情況下,使用的終端自動配置通過登錄或getty程序。有時,自動配置過程中猜測終端不正確。
如果終端設(shè)置不正確,命令的輸出可能看起來很奇怪,或者可能無法正確與shell交互。
為了確保,這是沒有的情況下,大多數(shù)用戶他們的終端設(shè)置到最低共同如下:
$TERM=vt100 $
當(dāng)輸入任何命令在命令提示符下,shell也有定位才可以執(zhí)行的命令。
PATH變量中指定位置的的shell看起來應(yīng)該命令。一般它被設(shè)置如下:
$PATH=/bin:/usr/bin $
這里每一個由冒號分開的各個條目,目錄。如果要求的shell來執(zhí)行命令并不能找到它在PATH變量中的任何目錄中,出現(xiàn)類似下面的消息:
$hello hello: not found $
有變量,如PS1和PS2在下一節(jié)討論。
命令提示符下的shell顯示為字符存儲在變量PS1。可以改變這個變量是你想要的任何東西。只要改變它,它會被用來由shell,從這一點(diǎn)上。
例如,如果發(fā)出命令:
$PS1='=>' => => =>
將成為提示=>。要設(shè)置PS1的價值,因此,它顯示的工作目錄,發(fā)出以下命令:
=>PS1="[u@h w]$" [root@ip-72-167-112-17 /var/www/yiibai/unix]$ [root@ip-72-167-112-17 /var/www/yiibai/unix]$
此命令的結(jié)果是,提示顯示用戶的用戶名,機(jī)器名(hostname),工作目錄。
有相當(dāng)多的PS1的值參數(shù)可以用來作為轉(zhuǎn)義序列,試圖限制了,提示沒有太多的信息。
Escape Sequence | 描述 |
---|---|
Current time, expressed as HH:MM:SS. | |
d | Current date, expressed as Weekday Month Date |
Newline. | |
s | Current shell environment. |
W | Working directory. |
w | Full path of the working directory. |
u | Current user.s username. |
h | Hostname of the current machine. |
# | Command number of the current command. Increases with each new command entered. |
$ | If the effective UID is 0 (that is, if you are logged in as root), end the prompt with the # character; otherwise, use the $. |
可以使自己的變化,每次登錄時,或者可以將它添加到您的配置文件中所做的更改會自動在PS1。
當(dāng)你發(fā)出一個命令,是不完整的,shell將顯示輔助提示,等待完成該命令,然后再次按Enter鍵。
默認(rèn)的次級提示>(大于號),但可以改變重新定義PS2 shell變量:
下面的例子使用默認(rèn)的次級提示:
$ echo "this is a > test" this is a test $
下面的例子重新定義PS2定制提示:
$ PS2="secondary prompt->" $ echo "this is a secondary prompt->test" this is a test $
以下是部分重要的環(huán)境變量列表。如上所述,這些變量將被設(shè)置和訪問:
變量 | 描述 |
---|---|
DISPLAY | Contains the identifier for the display that X11 programs should use by default. |
HOME | Indicates the home directory of the current user: the default argument for the cd built-in command. |
IFS | Indicates the Internal Field Separator that is used by the parser for word splitting after expansion. |
LANG | LANG expands to the default system locale; LC_ALL can be used to override this. For example, if its value is pt_BR, then the language is set to (Brazilian) Portuguese and the locale to Brazil. |
LD_LIBRARY_PATH | On many Unix systems with a dynamic linker, contains a colon-separated list of directories that the dynamic linker should search for shared objects when building a process image after exec, before searching in any other directories. |
PATH | Indicates search path for commands. It is a colon-separated list of directories in which the shell looks for commands. |
PWD | Indicates the current working directory as set by the cd command. |
RANDOM | Generates a random integer between 0 and 32,767 each time it is referenced. |
SHLVL | Increments by one each time an instance of bash is started. This variable is useful for determining whether the built-in exit command ends the current session. |
TERM | Refers to the display type |
TZ | Refers to Time zone. It can take values like GMT, AST, etc. |
UID | Expands to the numeric user ID of the current user, initialized at shell startup. |
以下是幾個環(huán)境變量的樣本示例:
$ echo $HOME /root ]$ echo $DISPLAY $ echo $TERM xterm $ echo $PATH /usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local/bin $