Unix 简明教程
Unix / Linux - Environment
在本章中,我们将详细讨论 Unix 环境。一个重要的 Unix 概念是 environment ,它由环境变量定义。有些是由系统设置的,有些是你设置的,还有些是由 shell 或加载另一个程序的任何程序设置的。
In this chapter, we will discuss in detail about the Unix environment. An important Unix concept is the environment, which is defined by environment variables. Some are set by the system, others by you, yet others by the shell, or any program that loads another program.
变量是一个我们向其分配值的字符串。分配的值可以是数字、文本、文件名、设备或任何其他类型的数据。
A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data.
例如,我们首先设置变量 TEST,然后使用 echo 命令访问其值 −
For example, first we set a variable TEST and then we access its value using the echo command −
$TEST="Unix Programming"
$echo $TEST
产生以下结果。
It produces the following result.
Unix Programming
请注意,设置环境变量时不用 $ 标号,但访问它们时用的前缀是 $ 标号。这些变量保留其值,直到我们退出 shell。
Note that the environment variables are set without using the $ sign but while accessing them we use the $ sign as prefix. These variables retain their values until we come out of the shell.
登录系统时,shell 会经过称为 initialization 的阶段来设置环境。这通常是一个分两步的过程,涉及 shell 读取以下文件−
When you log in to the system, the shell undergoes a phase called initialization to set up the environment. This is usually a two-step process that involves the shell reading the following files −
-
/etc/profile
-
profile
此过程如下−
The process is as follows −
-
The shell checks to see whether the file /etc/profile exists.
-
If it exists, the shell reads it. Otherwise, this file is skipped. No error message is displayed.
-
The shell checks to see whether the file .profile exists in your home directory. Your home directory is the directory that you start out in after you log in.
-
If it exists, the shell reads it; otherwise, the shell skips it. No error message is displayed.
这两个文件都已读取完毕后,shell 会显示一个提示−
As soon as both of these files have been read, the shell displays a prompt −
$
这是一个提示,您可以在此处输入命令,以让他们执行。
This is the prompt where you can enter commands in order to have them executed.
Note −此处详细说明的 shell 初始化过程适用于所有 Bourne 类型的 shell,但 bash 和 ksh 使用了一些另外的文件。
Note − The shell initialization process detailed here applies to all Bourne type shells, but some additional files are used by bash and ksh.
The .profile File
文件 /etc/profile 由 Unix 电脑的系统管理员维护,其中包含系统上的所有用户所需的 shell 初始化信息。
The file /etc/profile is maintained by the system administrator of your Unix machine and contains shell initialization information required by all users on a system.
文件 .profile 受您控制。您可以向此文件添加任意多的 shell 自定义信息。您需要配置的最少信息包括−
The file .profile is under your control. You can add as much shell customization information as you want to this file. The minimum set of information that you need to configure includes −
-
The type of terminal you are using.
-
A list of directories in which to locate the commands.
-
A list of variables affecting the look and feel of your terminal.
你可以在家目录中查看 .profile 。使用 vi 编辑器将其打开,并查看为你的环境设置的所有变量。
You can check your .profile available in your home directory. Open it using the vi editor and check all the variables set for your environment.
Setting the Terminal Type
通常,你正在使用的终端类型会由 login 或 getty 程序自动配置。有时,自动配置过程会错误猜测你的终端。
Usually, the type of terminal you are using is automatically configured by either the login or getty programs. Sometimes, the auto configuration process guesses your terminal incorrectly.
如果你的终端设置错误,则命令的输出可能看起来很奇怪,或者你可能无法与 shell 正确交互。
If your terminal is set incorrectly, the output of the commands might look strange, or you might not be able to interact with the shell properly.
为了确保这种情况不会发生,大多数用户以以下方式将他们的终端设置为最低公分母 −
To make sure that this is not the case, most users set their terminal to the lowest common denominator in the following way −
$TERM=vt100
$
Setting the PATH
当你 فى موجه الأوامر تكتب أى أمر، يتعين على الشل العثور على الأمر قبل تنفيذه.
When you type any command on the command prompt, the shell has to locate the command before it can be executed.
يوضح متغير PATH المواقع التى يجب أن يبحث فيها الشل عن الأوامر. وعادة ما يتم تعيين متغير المسار كما يلى −
The PATH variable specifies the locations in which the shell should look for commands. Usually the Path variable is set as follows −
$PATH=/bin:/usr/bin
$
هنا، كل مدخلة فردية مفصولة بواسطة حرف النقطتين (:) هى دلائل. إذا طلبت من الشل تنفيذ أمر ولم يتمكن من العثور عليه فى أى من الدلائل المحددة فى متغير المسار، ستظهر رسالة مشابهة لما يلى −
Here, each of the individual entries separated by the colon character (:) are directories. If you request the shell to execute a command and it cannot find it in any of the directories given in the PATH variable, a message similar to the following appears −
$hello
hello: not found
$
هناك متغيرات مثل PS1 و PS2 يتم مناقشتها فى القسم التالى.
There are variables like PS1 and PS2 which are discussed in the next section.
PS1 and PS2 Variables
يتم تخزين الأحرف التى يعرضها الشل على أنه موجه للأوامر فى متغير PS1. يمكنك تغيير هذا المتغير إلى أى شئ تريده. وبمجرد تغييره، سيستخدمه الشل من تلك النقطة فصاعدًا.
The characters that the shell displays as your command prompt are stored in the variable PS1. You can change this variable to be anything you want. As soon as you change it, it’ll be used by the shell from that point on.
على سبيل المثال، إذا أصدرت الأمر −
For example, if you issued the command −
$PS1='=>'
=>
=>
=>
سيصبح موجه الأوامر الخاص بك ⇒. لتعيين قيمة PS1 بحيث تعرض دليل العمل، أصدر الأمر −
Your prompt will become ⇒. To set the value of PS1 so that it shows the working directory, issue the command −
=>PS1="[\u@\h \w]\$"
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$
ويتمخض عن هذا الأمر إظهار موجه المستخدم لاسم المستخدم واسم الجهاز (اسم المضيف) ودليل العمل.
The result of this command is that the prompt displays the user’s username, the machine’s name (hostname), and the working directory.
يوجد عدد غير قليل من escape sequences التى يمكن استخدامها كحجج قيمة لـ PS1؛ حاول أن تقصر نفسك على الأكثر أهمية حتى لا يغمرك الموجه بالمعلومات.
There are quite a few escape sequences that can be used as value arguments for PS1; try to limit yourself to the most critical so that the prompt does not overwhelm you with information.
Sr.No. |
Escape Sequence & Description |
1 |
\t Current time, expressed as HH:MM:SS |
2 |
\d Current date, expressed as Weekday Month Date |
3 |
\n Newline |
4 |
\s Current shell environment |
5 |
\W Working directory |
6 |
\w Full path of the working directory |
7 |
\u Current user’s username |
8 |
\h Hostname of the current machine |
9 |
\# Command number of the current command. Increases when a new command is entered |
10 |
\$ If the effective UID is 0 (that is, if you are logged in as root), end the prompt with the # character; otherwise, use the $ sign |
每次登录时,您都可以自己进行更改,或者可以在 .profile 文件中添加更改使其在 PS1 中自动进行。
You can make the change yourself every time you log in, or you can have the change made automatically in PS1 by adding it to your .profile file.
发出不完整的命令时,外壳将显示一个次要提示符,并等待您完成命令并再次按下 Enter 。
When you issue a command that is incomplete, the shell will display a secondary prompt and wait for you to complete the command and hit Enter again.
默认的次要提示符是 > (大于号),但可以通过重新定义 PS2 外壳变量进行更改 −
The default secondary prompt is > (the greater than sign), but can be changed by re-defining the PS2 shell variable −
以下是使用默认次要提示符的示例 −
Following is the example which uses the default secondary prompt −
$ echo "this is a
> test"
this is a
test
$
下面给出的示例使用自定义提示符重新定义了 PS2 −
The example given below re-defines PS2 with a customized prompt −
$ PS2="secondary prompt->"
$ echo "this is a
secondary prompt->test"
this is a
test
$
Environment Variables
以下是重要环境变量的部分列表。这些变量按如下所述设置和访问 −
Following is the partial list of important environment variables. These variables are set and accessed as mentioned below −
Sr.No. |
Variable & Description |
1 |
DISPLAY Contains the identifier for the display that X11 programs should use by default. |
2 |
HOME Indicates the home directory of the current user: the default argument for the cd built-in command. |
3 |
IFS Indicates the Internal Field Separator that is used by the parser for word splitting after expansion. |
4 |
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. |
5 |
LD_LIBRARY_PATH A Unix system with a dynamic linker, contains a colonseparated 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. |
6 |
PATH Indicates the search path for commands. It is a colon-separated list of directories in which the shell looks for commands. |
7 |
PWD Indicates the current working directory as set by the cd command. |
8 |
RANDOM Generates a random integer between 0 and 32,767 each time it is referenced. |
9 |
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. |
10 |
TERM Refers to the display type. |
11 |
TZ Refers to Time zone. It can take values like GMT, AST, etc. |
12 |
UID Expands to the numeric user ID of the current user, initialized at the shell startup. |
以下是显示几个环境变量的示例 -
Following is the sample example showing few environment variables −
$ echo $HOME
/root
]$ echo $DISPLAY
$ echo $TERM
xterm
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local/bin
$