Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

3.5 Important environmental variables


May 23, 2021 That's what Linux should learn



A variable is the type of data that a computer system holds for variable values. I n Linux systems, variable names are generally capital, which is a convention. W e can extract the corresponding variable value directly from the variable name. Environment variables in a Linux system are used to define some parameters of the system's operating environment, such as each user's different home directory, mail storage location, and so on.

Careful readers should find that the title names in this and the last sections are adjectives - important and common. The reason is self-evident - for Linux systems to work properly and serve users, hundreds of environment variables are needed to work together, so we don't have to look at and learn each variable one by one, but rather focus on the most important things in a limited space.

Hundreds of variables work together to help Linux systems build a working environment that provides services to users through environment variables. Y ou certainly don't have to look at every variable, but you should focus on what's most important to your readers in your most valuable books. I n order to better help you understand the role of variables, Mr. Liu Wei gave you an example. A s noted earlier, everything in a Linux system is a file, and Linux commands are no exception. S o what happened to the Linux system after the user executed a command? Simply put, command execution in Linux is divided into four steps.

Step 1: Determine whether the user enters commands (e.g. /bin/ls) in absolute or relative paths and, if so, executes them directly.

Step 2: The Linux system checks that the command entered by the user is an "alias command", which replaces the original command name with a custom command name. Y ou can use the alias command to create a command alias of your own, in the format "alias alias. T o cancel a command alias, use the unlias command in the format "unalias alias". When we used the rm command to delete files, the Linux system would ask us to confirm whether to delete the file again, in fact, this is the Linux system to prevent users from deleting the file by mistake and deliberately set the rm alias command, and then we cancel it:

[root@linuxprobe ~]# ls anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates Desktop Downloads Music Public Videos [root@linuxprobe ~]# rm anaconda-ks.cfg rm: remove regular file ‘anaconda-ks.cfg’? y [root@linuxprobe~]# alias rm alias rm='rm -i' [root@linuxprobe ~]# unalias rm [root@linuxprobe ~]# rm initial-setup-ks.cfg [root@linuxprobe ~] #

Step 3: The Bash interpreter determines whether the user is entering an internal command or an external command. I nternal commands are instructions inside the interpreter that are executed directly, while the user enters external commands most of the time, which are handed over to Step 4 to continue processing. You can use the type command name to determine whether the command entered by the user is an internal command or an external command.

Step 4: The system looks for user-entered command files in multiple paths, and the variables that define these paths, called PATH, can simply be understood as "interpreter's little assistant" to tell the Bash interpreter where the commands to be executed might be stored, and then the Bash interpreter will look up them one by one in those locations. PATH is a variable consisting of multiple path values, each with a colon interval, and the addition and deletion of these paths will affect the Bash interpreter's search for Linux commands.

[root@linuxprobe ~]# echo $PATH /usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin [root@linuxprobe ~]# PATH=$PATH:/root/bin [root@linuxprobe ~]# echo $PATH /usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin

Here's the classic question: "Why can't you add the current directory (.) to PATH?" T he reason is that although the current directory (.) can be added to the PATH variable, in some cases the user can avoid the hassle of entering the path in which the command is located. However, if a hacker holds a Trojan file with the same name as the ls or cd command in the more commonly used public directory/tmp, and the user happens to execute those commands in the public directory, it is highly likely that the trick will be made.

So, as a cautious, experienced operations staff, after taking over a Linux system, you must check the PATH variable for suspicious directories before executing commands, and whether the reader also feels that the environment variable is particularly useful from the previous EXAMPLE example of the PATH variable. We can use the env command to see all the environment variables in the Linux system, and Mr. Liu has carefully selected the 10 most important environment variables for you, as shown in Table 3-3.

The 10 most important environment variables in Table 3-3 Linux systems

Variable Name Acting HOME user's home directory (i.e. home directory) SHELL user in the use of shell interpreter name HISTSIZE output of the historical command record number HISTFILESIZE saved history command record number MAIL message save path LANG system language, language name RANDOM generates a random number PS1 Bash interpreter prompt PATH definition interpreter to search the path of the user to execute the command EDITOR The user's default text editor

Linux, as a multi-user, multi-tasking operating system, provides each user with a separate, appropriate working environment, so an identical variable has different values depending on the user's identity. For example, let's use the following command to see what values the HOME variable has for different user identities (su is a command for switching user identities, which we'll meet in Chapter 5):

[root@linuxprobe ~]# echo $HOME /root [root@linuxprobe ~]# su - linuxprobe Last login: Fri Feb 27 19:49:57 CST 2017 on pts/0 [linuxprobe@linuxprobe ~]$ echo $HOME /home/linuxprobe

In fact, variables are composed of fixed variable names and variable values set by the user or system, and we can create our own variables to meet our work needs. For example, set a variable named WORKDIR to make it easier for users to access a deeper directory:

[root@linuxprobe ~]# mkdir /home/workdir [root@linuxprobe ~]# WORKDIR=/home/workdir [root@linuxprobe ~]# cd $WORKDIR [root@linuxprobe workdir]# pwd /home/workdir

However, such variables are not global and have a limited scope of action and cannot be used by other users by default. If the job requires it, you can use the export command to promote it to a global variable so that other users can use it:

[root@linuxprobe workdir]# su linuxprobe Last login: Fri Mar 20 20:52:10 CST 2017 on pts/0 [linuxprobe@linuxprobe ~]$ cd $WORKDIR [linuxprobe@linuxprobe ~]$ echo $WORKDIR [linuxprobe@linuxprobe ~]$ exit [root@linuxprobe ~]# export WORKDIR [root@linuxprobe ~]# su linuxprobe Last login: Fri Mar 20 21:52:10 CST 2017 on pts/0 [linuxprobe@linuxprobe ~]$ cd $WORKDIR [ linuxprobe@linuxprobe workdir]$ pwd /home/workdir