environment variables remember the last chapter, I have mentioned:
When we log in to the system, we first get a shell, and it also takes a trip (process), and then enter The commands belong to the subroutine (child process) of this shell. If you are careful enough, it is not difficult to find that our shell is set in the /etc/passwd file, which is the last column of the account settings. The default is /bin/bash.
In fact, when we get a shell, we can really communicate with the system, such as entering your commands, executing your program, and so on. You can also enter another shell (that is, start a subroutine) after getting a shell, and then you can go to a deeper shell (and then into the subroutine of the subroutine) until you enter exit. In the previous shell (return the parent program of the previous level). If you have read the subroutine concepts mentioned in the previous chapter, it should be easy to understand. However, your behavior is not unlimited, and many settings must be defined in advance. So, when you get the shell, you also get some environment settings, or "Environment Variables". The so-called variable (variable) is to save a certain set value with a specific name (or label), and then use it for future use. For example, surname = chen; name = kenny, then ‘last name & rsquo; & & lsquo; name & rsquo; is the variable name, and chen and kenny are the values held by the variable. The variables defined and managed by the shell are called environment variables because they can be used by all subroutines generated by the shell. The environment variable names are generally represented by uppercase letters. For example, our common environment variables have these: (Note: that is, the environment variable is the variable used by all the child processes generated by the shell) The variable name represents the current command of HISTCMD. Record number. HISTFILE command record file storage file. The HISTSIZE command records the table volume. HOME default login home directory. IFS preset separator. LINENO The current number of lines in the shell script. The path to the MAIL mail box. The number of seconds that MAILCHECK checks the message. The path to the directory where OLDPWD last entered. OSTYPEOS
type. The PATH default command search path. PID of the PPID parent program. PWD current working directory path. The continuous startup time of the current shell of SECONDS. SHELL current shell execution path. The maximum idle time for TMOUT to automatically log off. UID of the UID user. $ Current shell PID. ? The return status of the last command. If you want to see what these variable values are, just put a "$” symbol in front of the variable name, and then use the echo command to view it: # echo$PWD /root # echo $$ 1206 # echo $ The first command is to display the path of the current directory, which is the same as the result of executing the pwd command. The second command displays the current shell's PID, which is 1206. If you enter kill -9 1206 at this time, the current shell will be cut off, then you have to log in again to get another shell, and its PID is also new; the third line command is the return status of the previous command. : If the command is executed successfully and there is no error, it is usually 0; if the command encounters an error, the return status is non-zero, and its value depends on the programmer (we will introduce it later in the shell script). For the last command, compare the following results: # ls mbox mbox # echo $? 0 # ls no_mbox ls: no_mbox: No such file or directory # echo $? 1 You will find: the first command executed successfully, so it returns The state is 0; the second command fails to execute and its return state is 1. If the programmer sets a different return status level for different errors, then you can derive from the return value what kind of error is causing the problem. Tips: If you write a program or script later, you should develop a habit of setting the return status for each command result. This is very important, especially when debugging. Let's talk about this when we learn script later. (This is important to get attention!) We can always use a = (equal sign) to define a new variable or change an existing variable. For example: # MYNAME=kenny # echo $MYNAME kenny If you want to cancel a defined variable, you can use the unset command: # unset MYNAME However, one of the characteristics of the environment variable is one-way output. That is: a shell-specific variable can only be used in this shell. If you want to share it with other programs, scripts, commands, or their subroutines in the same shell, you must use the export command to output this variable. But anyway, if you define a variable in a subroutine, the value of this variable only affects the subroutine itself and its own subroutine, and never images to the parent or other children generated by the parent. program. (This is why we usually use the export command to modify the PATH path. For example, you define a new variable in a program, or change the value of an existing variable. At the end of the program, it is set. The variables are all canceled; if you want to share the value of the variable to the subroutine generated by the program, you must use the export command to retain the value of the variable, unless the subroutine redefines it. But in any case, the value of the variable defined by the current program cannot be passed back to the parent program. Let's do the following experiment: # MYNAME=kenny # echo $MYNAME kenny # export MYNAME # Set a variable. # # Current set value. # Use export to output variable values. # /bin/bash # Open another shell, which is to enter the subroutine. # echo $MYNAME kenny # # Keep the original settings. # export MYNAME=netman # echo $MYNAME netman # Redefine the set value and also use the export output. # # The variable value is replaced by the new value. # exit # Exit the subroutine and return to the parent program. # echo $MYNAME kenny # # The variable value of the parent program has not changed. Regarding another property of a variable, the value of the variable is inheritable. That is, you can set a variable value to set another variable name. Let's say: # FIRST_NAME="Kenny" # MYNAME=$FIRST_NAME# echo $MYNAME Kenny# Define a variable. # Define another variable, but its value is the first variable. # # The second variable inherits the value of the first variable. In addition, the definition of variables when you also pay attention to a few rules:
& middot; When you define a variable, & ldquo; = & rdquo; no numbers on both sides of the spacebar;
& middot; as the name of the variable, only Letters and numbers, but not numbers; if the name is too long, it can be separated by “_”;
· The predefined variables are all uppercase, and the custom variables can be mixed case to better distinguish; Br>· Only variables defined by the shell itself can be called environment variables;
· If the variable has special characters, it must first be skipped with “\\”;
· There are blanks, you must use quotes, or you can jump off.
About the last two, maybe we will find some examples to experience: # TOPIC='Q& A'# Keep special symbols and blanks with single quotes # Q1=What\\'s\\your\\ \\"topic\\ "\\?# echo $Q1 What's your "topic"? # Use \\ to jump off special symbols (with quotes) and whitespace # # After jumping off, special symbols and whitespace are preserved. # ANS="Itis $TOPIC." ------ This place draws attention: strings in double quotes can reference variables! # echo $ANS ------ This usage is more common, and the following single quotes are not common. It is Q & A. # Use double quotes to retain variable values ($) # # Use double quotes to display variable values. # WRONG_ANS='Itis "$TOPIC".' ----- Note the reasonable use of single and double quotes # echo $WRONG_ANS It is "$TOPIC". # Keep special symbols and blanks with single quotes (same as first Line) # # Use single quotes, all reserved; at the same time: # $ is also reserved as a general symbol, not a variable value. ------ This place draws attention. When single quotes, $ is reserved as a general symbol! # ALT_ANS='the $TOPIC'\\ is\\ "'$TOPIC'"\\. ------ Mixing single and double quotes helps to understand. # echo $ALT_ANS The $TOPIC is 'Q & A'. # Mixed single quotes, double quotes, and escape characters \\ # #单引号留所有; double quotes retain variable values; # \\ Jump out special symbols . Let me explain the last example here: 'the $TOPIC is '"$TOPIC"\\. First enclose the text 'the $TOPIC is ' in single quotes, using 3 spaces and a $ symbol; then use double quotes to retain the value of $TOPIC; and finally use \\ to escape the decimal point. When referring to the " " and ' ' symbols, basically, the content enclosed by ' ' will become a single string, and any special character will lose its special function and become a general character, but You can no longer use the 'symbol, but in the middle of " ", there is no ''so strict, some special characters, such as $, still retain its special features. You might as well take a look at the difference between echo ' "$HOME" ' and echo " '$HOME' ". Tips: The escape character in the shell command line “ \\ ” In fact, we will often use it. For example, if one of your commands is too long, it may be more than one line, or you want to enter the command line neatly, you may want to press Enter to type the next line to continue typing. However, when you hit the Enter key, you actually enter a CR (Carriage-Return) character. Once the shell reads the CR character, it will try to execute the command. At this point, you can type the \\ symbol before entering Enter, and you will be able to jump out of the CR character, so the shell will not execute the command immediately. This command line is often seen in scripts, but you have to know what that means.