Each line read by shell from a standard input or script is called a pipeline; it contains one or more commands ), these commands are separated by one or more pipe characters (|.
In fact, there are many special symbols that can be used to separate a single command: semicolon (;), pipeline (|), &, logic and (&), and logic or (| ). For each read pipeline, shell splits the command back, sets I/O for the pipeline, and performs the following operations on each command in sequence:
As shown in the following figure, the steps are complex. When the command line is processed, each step occurs in the shell memory; the shell will not display the occurrence of each step to you. Therefore, you can assume that we have a glimpse of the shell memory to know how the command lines at each stage are converted. Let's start with this example:CopyCodeThe Code is as follows: $ mkidr/tmp/X creates a temporary directory
$ CD/tmp/X switch to this directory
$ Touch F1 F2 create a file
$ F = f y = "a B" assigns two variables.
$ Echo ~ +/$ {F} [12] $ y $ (echo cmd SUBST) $(3 + 2)> out redirects the result to out
The preceding steps are as follows:
1. Split the command into tokens Based on Shell syntax at the beginning. The most important thing is that I/O redirection> out is identified here and stored for later use. The process continues to process the following line, where the range of each token is displayed on the line below the command:
Echo ~ +/$ {F} [12] $ y $ (echo cmd SUBST) $(3 + 2 ))
| 1 | ----- 2 ---- | 3 | -------- 4 ---------- | ---- 5 ----- |
2. Check whether the first word (ECHO) is a keyword, such as if or. No, so the command line will not change.
3. Check whether the first word (ECHO) is an alias. Not here. Therefore, the command line remains unchanged and the process continues.
4. Do you need to expand the Tilde for scanning words. In this example ,~ + The extension of ksh93 and bash is equivalent to $ PWD, which is the current directory. Token 2 will be modified and processed as follows:
Echo/tmp/X/$ {f} [12] $ y $ (echo cmd SUBST) $(3 + 2 ))
| 1 | ------- 2 ------- | 3 | -------- 4 ---------- | ---- 5 ----- |
5. The next step is to expand the variable: Both token 2 and Token 3 are modified. This will produce:
Echo/tmp/X/$ {f} [12] a B $ (echo cmd SUBST) $(3 + 2 ))
| 1 | ------- 2 ------- | 3 | -------- 4 ---------- | ---- 5 ----- |
6. Then, replace the command. Note: all the steps in the recursive Application List are available here! Here, the command replaces and modifies token 4:
Echo/tmp/X/$ {f} [12] a B cmd SUBST $(3 + 2 ))
| 1 | ------- 2 ------- | 3 | --- 4 ---- | ---- 5 ----- |
7. Execute arithmetic replacement now. Token 5 is modified. Result:
Echo/tmp/X/$ {f} [12] a B cmd SUBST 5
| 1 | ------- 2 ------- | 3 | --- 4 ---- | 5 |
8. All the preceding Expansion results will be scanned again to see if there is a $ ifs character. If yes, they are used as the separator (separator) to generate additional words. For example, the two characters $ y originally constitute a word, and the single expansion is "a-space-B ", this stage is divided into two words: A and B. The same method is also applied to the result of command $ (echo cmd SUBST. The previous token 3 is changed to token 3 and
Token 4. The previous token 4 is token 5 and Token 6. Result:
Echo/tmp/X/$ {f} [12] a B cmd SUBST 5
| 1 | ------- 2 ------- | 3 4 |-5-|-6-| 7
9. The final replacement phase is wildcard expansion. Token 2 is changed to token 2 and Token 3:
Echo/tmp/X/$ F1/tmp/X/$ F2 a B cmd SUBST 5
| 1 | ---- 2 ---- | ---- 3 ---- | 4 5 |-6-|-7-| 8
10. At this time, shell is ready to execute the final command. It will search for ECHO. It happens that both ksh93 and bash ECHO are built into shell.
11. Shell actually executes the command. First, execute> out I/O redirection, and then call the internal echo version to display the final parameter.
Final result:Copy codeThe Code is as follows: $ cat out
/Tmp/X/F1/tmp/X/F2 a B cmd SUBST 5