Article Title: Exploring the value behind environment variables in unix. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Environment variables can act on the user's entire work environment. At the same time, related applications also need to use these environment variables to obtain the environment variables for running processes. It is not as good as the mail monitoring program. By default, it will not wait a while to check whether there are new emails. To implement this function, the mail monitoring program must know the location of the mail storage and the monitoring frequency. These two things are implemented through the environment variables of the Unix system. However, in practice, many Unix System Engineers ignore the role of environment variables. Or, the value behind the environment variables is not mined. This article may give some inspiration to the majority of Unix system enthusiasts.
I. Role of the prompt.
Before talking about the value of the prompt, I will show you the display format of the dos prompt. As shown in, the DOS version is in Microsoft XP.
From this we can see that the DOS prompt is more intuitive. Start with the drive letter, then the current absolute path, and end with the> symbol. But not in Unix systems. In Unix systems, the path does not determine the path, but only displays the name of the current directory. This has caused some difficulties for normal maintenance. In a Unix system, can I display it in the DOS prompt format? Displays the current absolute path. To implement this function, you need to start from a Unix prompt.
The prompt information is also an environment variable in the Unix operating system. There are two prompts in the shell environment: PS1 and PS2. PS1 is the main prompt string, that is, the prompt that you usually see (may be a dollar sign $ or a # font size ). Generally, if the current user is a privileged user, the primary prompt may be #. If the current user is a common user, the primary prompt is the $ symbol. In addition, you can run multiple commands in a shell process. The system displays> prompt, which is a secondary prompt and is saved in the PS2 environment variable. So how can we use this prompt to meet the above requirements? Let me hear from you.
1. The first thing that needs to be displayed is that both the main prompt and secondary prompt end with>. In this case, the system engineer can change the value of the primary prompt. For example, you can run the $ PS1 = ">" command. This command is used to change the value of this environment variable. In addition, some people may also like to add the c: drive letter to the front. In this case, you can set the value of PS1 in the primary prompt to "c:> ". However, it should be noted that the Unix file system is different from Microsoft's file system. In a Unix operating system, there is only one directory with a tree structure. In a Windows operating system, each partition has a root directory with an independent tree structure. Therefore, C, D, and E disks are not distinguished in Unix operating systems.
2. The second step is to display the absolute path in the prompt. In this case, the PWD environment variable is required. In Linux, the pwd command can be used to display the current absolute path. However, in BASH in Unix operating systems (shell), a PWD environment variable is provided. This environment variable is mainly used to store the absolute path of the current cursor. PWD is a dynamic environment variable. Each time a user switches the directory, the environment variable is assigned a new value. To display the current absolute path in the prompt, you only need to assign this variable to ps1. Because of the dynamic change of PWD variables, the main prompt also changes with the change of PWD environment variables (absolute path. The specific value Assignment Command is: $ PS1 = '$ pwd '. Here I want to emphasize a small detail. When you assign an environment variable to another environment variable, you need to add the $ symbol. Otherwise, the Unix operating system treats the variable as a normal string. I often make mistakes when training students. This is because they may be affected by previous experience. When programming in some development environments, when assigning the value of a variable to other variables, you do not need to add special symbols such as $ before the variable. However, in Unix operating systems, if you assign the value of an environment variable to another environment variable, you must use the $ symbol to mark it, otherwise, the system treats the environment variable name as a common character.
In addition to simulating the display format of a DOS prompt, the prompt also plays an important role. For example, most System Engineers do not directly use the Unix terminal console to manage Unix servers. Instead, you can use ssh and other remote connection protocols for remote login and maintenance. If there are many Unix servers in the enterprise, system engineers may be in trouble. By default, ssh is used to remotely log on to the Unix operating system, and the Unix host name is not displayed on the terminal. Therefore, if System Engineers connect to multiple Unix servers at the same time, they will not be able to clearly know which Unix server is. In this case, the system engineer wants to display the Unix host name at the prompt. In the bash environment, there is an escape character that can meet similar requirements. The Escape Character \ h is used to display the name of the current host. To solve this problem, if the system engineer needs to prompt the host name, you can run the following command: $ PS1 = "\ h ". After this setting, the name of the current host is displayed at the Unix operating system prompt. In this regard, the host name is displayed by default in Linux.
[1] [2] Next page