Unix的一個(gè)重要概念是環(huán)境,被定義的環(huán)境變量。有些系統(tǒng)通過(guò)環(huán)境變量,還有一些由shell,或任何程序加載另一個(gè)程序。
變量是一個(gè)字符串,我們分配一個(gè)值。分配的值可以是一個(gè)數(shù)字,文本,文件名,移動(dòng)設(shè)備,或任何其他類型的數(shù)據(jù)。
例如,首先我們?cè)O(shè)定一個(gè)變量測(cè)試,然后我們使用echo命令來(lái)訪問(wèn)它的值:
$TEST="Unix Programming" $echo $TEST Unix Programming
需要注意的是環(huán)境變量設(shè)置,而無(wú)需使用$符號(hào),但訪問(wèn)它們時(shí),我們使用$符號(hào)作為前綴。這些變量保持它們的值,直到我們?cè)O(shè)計(jì)出來(lái)shell。
當(dāng)你登錄到系統(tǒng)中,shell經(jīng)過(guò)一個(gè)階段稱為初始化設(shè)置各種環(huán)境。這通常是一個(gè)兩步的過(guò)程,涉及的的shell閱讀下列文件:
/etc/profile
profile
過(guò)程如下:
shell進(jìn)行檢查,看是否存在文件 /etc/profile文件。
如果它存在,當(dāng)shell讀取。否則,此文件將被跳過(guò)。不顯示錯(cuò)誤消息。
shell檢查,看看是否該文件。配置文件存在于你的home目錄。主目錄是開(kāi)始在登錄后的目錄
如果它存在,當(dāng)shell讀取它,否則shell跳過(guò)。不顯示錯(cuò)誤消息。
只要這兩個(gè)文件被讀取,shell將顯示一個(gè)提示:
$
這是提示這里你可以輸入命令,以讓他們執(zhí)行。
注 - 這里詳述shell初始化過(guò)程適用于所有的Bourne型shell,但所使用的是bash和ksh一些額外的文件。
文件/etc/profile文件是由UNIX機(jī)器的系統(tǒng)管理員,包含shell初始化所需的信息系統(tǒng)上的所有用戶。
該文件 .profile是在你的控制之下。您可以添加盡可能多的 shell定制信息,只要想這個(gè)文件。最小信息集,需要配置包括:
使用的終端的類型
在其中定位命令的目錄的列表
列表變量,影響終端的外觀和風(fēng)格。
可以檢查 .profile,在home目錄。使用vi編輯器打開(kāi)它,并檢查所有的變量設(shè)置環(huán)境。
通常情況下,使用的終端自動(dòng)配置通過(guò)登錄或getty程序。有時(shí),自動(dòng)配置過(guò)程中猜測(cè)終端不正確。
如果終端設(shè)置不正確,命令的輸出可能看起來(lái)很奇怪,或者可能無(wú)法正確與shell交互。
為了確保,這是沒(méi)有的情況下,大多數(shù)用戶他們的終端設(shè)置到最低共同如下:
$TERM=vt100 $
當(dāng)輸入任何命令在命令提示符下,shell也有定位才可以執(zhí)行的命令。
PATH變量中指定位置的的shell看起來(lái)應(yīng)該命令。一般它被設(shè)置如下:
$PATH=/bin:/usr/bin $
這里每一個(gè)由冒號(hào)分開(kāi)的各個(gè)條目,目錄。如果要求的shell來(lái)執(zhí)行命令并不能找到它在PATH變量中的任何目錄中,出現(xiàn)類似下面的消息:
$hello hello: not found $
有變量,如PS1和PS2在下一節(jié)討論。
命令提示符下的shell顯示為字符存儲(chǔ)在變量PS1。可以改變這個(gè)變量是你想要的任何東西。只要改變它,它會(huì)被用來(lái)由shell,從這一點(diǎn)上。
例如,如果發(fā)出命令:
$PS1='=>' => => =>
將成為提示=>。要設(shè)置PS1的價(jià)值,因此,它顯示的工作目錄,發(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ù)可以用來(lái)作為轉(zhuǎn)義序列,試圖限制了,提示沒(méi)有太多的信息。
| 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 $. |
可以使自己的變化,每次登錄時(shí),或者可以將它添加到您的配置文件中所做的更改會(huì)自動(dòng)在PS1。
當(dāng)你發(fā)出一個(gè)命令,是不完整的,shell將顯示輔助提示,等待完成該命令,然后再次按Enter鍵。
默認(rèn)的次級(jí)提示>(大于號(hào)),但可以改變重新定義PS2 shell變量:
下面的例子使用默認(rèn)的次級(jí)提示:
$ 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è)置和訪問(wèn):
| 變量 | 描述 |
|---|---|
| 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. |
以下是幾個(gè)環(huán)境變量的樣本示例:
$ echo $HOME /root ]$ echo $DISPLAY $ echo $TERM xterm $ echo $PATH /usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local/bin $