Shell command execution sequence analysis [figure]_linux Shell

Source: Internet
Author: User
Each line that the Shell reads from standard input or script is called a pipe (pipeline); it contains one or more commands (commands) that are separated by one or more pipe characters (|).

In fact, there are many special symbols that can be used to separate individual commands: semicolons (;), Pipes (|), &, Logical AND (&&), and Logical OR (| | )。 For each of the read pipes, the shell splits the command back, sets the I/O to the pipe, and performs the following actions in sequence for each command:


The entire sequence of steps, as shown in the previous illustration, looks a bit complicated. When the command line is processed, each step occurs in the shell's memory; The shell does not really show you the occurrence of each step. So, you can assume that we're peeping through the shell's memory to see how the command line at each stage is converted. From this example, we start by saying:
Copy Code code as follows:

$ mkidr/tmp/x Create a temporary directory
$ cd/tmp/x Switch to this directory
$ touch F1 F2 build file
$ f=f y= "A B" assignment two variables
$ echo ~+/${f}[12] $y $ (echo cmd subst) $ ((3 + 2)) > out to redirect results to out


the above implementation steps are summarized as follows:

1. The command begins to be split into token according to the shell syntax. The most important point is that I/O redirection >out is recognized here and stored for later use. The process continues to process the following line, where each token range is displayed on the line below the command:

Echo ~+/${f}[12] $y $ (echo cmd subst) $ ((3 + 2))
| 1 | |-----2----| |3 | |--------4----------| |----5-----|

2. Check if the first word (echo) is a keyword, such as if or for. This is not the case, so the command line does not change to continue processing.
3. Check if the first word (echo) is an alias. Not here. So the command line does not change and continues processing.
4. Scan so that the word needs to expand the wave number. In this case, ~+ is an extension of ksh93 and bash, equivalent to $pwd, the current directory. Token 2 will be modified to handle the following:

ECHO/TMP/X/${F}[12] $y $ (echo cmd subst) $ ((3 + 2))
| 1 | |-------2-------| |3 | |--------4----------| |----5-----|

5. The next step is variable expansion: Token 2 and 3 are all modified. This will produce:

ECHO/TMP/X/${F}[12] A B $ (echo cmd subst) $ ((3 + 2))
| 1 | |-------2-------| | 3 | |--------4----------| |----5-----|

6. Again to deal with is the command replacement. Note that you can use recursively to apply all the steps in the list! Here, the command replacement modifies the token 4:

ECHO/TMP/X/${F}[12] A b cmd subst $ ((3 + 2))
| 1 | |-------2-------| | 3 | |---4----| |----5-----|

7. Now perform the arithmetic substitution. Modified by token 5, the result:

ECHO/TMP/X/${F}[12] A b cmd subst 5
| 1 | |-------2-------| | 3 | |---4----| |5|

8. All the previous unfolding results will be scanned again to see if there are any $IFS characters. If there are, then they are used as delimiters (separator) to produce extra words, for example, two characters $y originally formed a word, the single expansion "A-space-B", at this stage was cut into two words: A and B. The same method is also applied to the results of the command $ (echo cmd subst). The previous token 3 became token 3 and
Token 4. The previous token 4 became token 5 and token 6. Results:

ECHO/TMP/X/${F}[12] A b cmd subst 5
| 1 | |-------2-------| 3 4 |-5-| |-6-| 7

9. The final replacement phase is the wildcard expansion. Token 2 became token 2 and Token 3:

echo/tmp/x/$f 1/tmp/x/$f 2 a b cmd subst 5
| 1 | |----2----| |----3----| 4 5 |-6-| |-7-| 8

10. At this point, the shell is ready to execute the final command. It will go looking for Echo. Exactly ksh93 and Bash's echo are built into the shell.

11.Shell actually executes the command. First perform the I/O redirection of > out, and then invoke the internal ECHO version to display the final argument.

The final result:
Copy Code code as follows:

$cat out
/tmp/x/f1/tmp/x/f2 a b cmd subst 5
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.