This article is also the seventh chapter of "Learning the bash Shell" 3rd Edition, "Input/Output and Command-Line Processing", four of the Reading Notes. Our goal is to read books thin. We will examine how the Linux system parses bash.
Step 1
: The command is assigned as a token based on spaces, tabs, NEWLINE,;, (,), <,>, |, and &.
Step 2
: Check whether the first token has some keywords, whether it is quotation marks, and whether '/' is a symbol to be combined, such as '{' or '(' to see if you need to read the next line of command, if necessary, go back to step 1st, so that the loop continues until the complete command is obtained. If it is not the start of the combined command, but other keywords, such as the end fi, report a syntax error. If not, go to step 1.
Step 3
: Check the 1st tocken of the command according to the alias list. It gets the alias correspondence and returns to step 1st. Go back to Step 1 and allow alias nesting. A represents B, B represents C, and alias represents the beginning of some integration commands.
Step 4
: Braces. For example, resolve a {B, c} to AB ac.
Step 5
: Wave number (~) Parsing, if at the beginning of a word, it is interpreted as the $ HOME directory path of the user.
Step 6
: Parameter Parsing. If the word starts with $, parse this parameter variable.
Step 7
: Whether to generate command replacement for $ parsing.
Step 8
: Performs mathematical operations on the format of $ (string.
Step 9
: After several steps starting from step 1, separate words again and use $ IFS as the separator.
Step 10
: Path name processing, wildcard processing, including *,?, /.
Step 2
: Check the command according to the first word of the command. The priority is a function command, builtin, and $ PATH command.
Step 2
: Execute the command after setting redirection I/O
This is only some of the steps, not all. An example is provided for further explanation. Before executing this command, you can use alias ll = "ls-l ". The user directory is/home/alice, and the $ value is 2537. Run ll $ (type-path cc )~ Alice/. * $ ($ % 1000.
- Ll
$ (Type
-Path
Cc)
~ Alice/. * $ ($ % 1000 ))
Words
Ll
It is not keyword, so the second step does not need to process any
Ls
-L
$ (Type
-Path
Cc)
~ Alice/. * $ ($ % 1000 ))
Replace ls-l with the alias "ll". This will repeat Step 1 to step 3.
Ls
-L
$ (Type
-Path
Cc)
~ Alice/. * $ ($ % 1000 ))
Do not process any
Ls
-L
$ (Type
-Path
Cc)
/Home/alice/. * $ ($ % 1000 ))
To parse the path ~ Alice resolves/Home/alice
.
Ls
-L
$ (Type
-Path
Cc)
/Home/alice/. * $ (2537% 1000 ))
Concatenates $ to 2537. Learn how to use $ later.
Ls
-L
/Usr/bin/cc
/Home/alice/. * $ (2537% 1000 ))
Run the command to replace "type-path cc ".
Ls
-L
/Usr/bin/cc
/Home/alice/. * 537
Perform arithmetic operations.
Ls
-L
/Usr/bin/cc
/Home/alice/. * 537
No processing is performed in Step 9.
Ls
-L
/Usr/bin/cc
/Home/alice/. hist537
Replace the wildcard with the file name. * 537.
In/Usr/bin
To obtain the ls command
/Usr/bin/ls
Is executed with option-l and two parameters
Link: Me
Linux operations related articles