I. Shell INTRODUCTION
The role of the shell is to explain the execution of the user's command, the user enters a command, the shell executes one, which is called Interactive, the shell has a way to execute the command is batch processing, the user wrote a shell script, let the shell once all the commands to execute once.
Two. Shell execution script
The shell script uses the # notation, but the first row is the exception, which indicates that the script uses the interpreter specified later to interpret the execution/bin/sh. How shell scripts work:
Interactive shell (bash) fork/exec a child shell (SH) is used to execute a script, and the parent process bash waits for the child process sh to terminate.
2. SH read the CD in the script. command, call the appropriate function to execute the built-in command, changing the current working directory to the previous level.
3. SH reads the LS command in the script, fork/exec This program, lists the files under the current working directory, SH waits for LS to terminate.
4. After the LS terminates, SH continues to execute, reading to the end of the script file, SH terminates.
650) this.width=650; "title=" 1.png "style=" Float:none; "src=" http://s2.51cto.com/wyfs02/M02/86/2F/ Wkiol1e2-cah4kcxaabgsnvhmom594.png-wh_500x0-wm_3-wmp_4-s_2612253039.png "alt=" Wkiol1e2-cah4kcxaabgsnvhmom594.png-wh_50 "/>
650) this.width=650; "title=" 2.png "style=" Float:none; "src=" http://s2.51cto.com/wyfs02/M00/86/2F/ Wkiol1e2-cfzf-ckaaasglsm1f4642.png-wh_500x0-wm_3-wmp_4-s_158382793.png "alt=" Wkiol1e2-cfzf-ckaaasglsm1f4642.png-wh_50 "/>
5. When SH terminates, bash resumes execution, and the print prompt waits for the user to lose.
If you enclose commands entered under the command line in parentheses, you will also fork out a child shell to execute the command in parentheses.
650) this.width=650; "title=" 3.png "src=" http://s3.51cto.com/wyfs02/M00/86/2F/wKiom1e29_ Ognuwbaaahwujzjkg882.png-wh_500x0-wm_3-wmp_4-s_3310929200.png "alt=" wkiom1e29_ognuwbaaahwujzjkg882.png-wh_50 "/ >
The CD, source, or. command is the shell's built-in command, which does not create a child shell, but rather follows the line-by-row command of the script directly under the interactive shell.
Three. Shell variables
Shell variables consist of all caps and underscores, and there are two types of shell variables:
Environment variables
Environment variables can be passed from the parent process to the child process, so the environment variables of the shell process can be passed from the current shell process to the fork
Out of the child process. Use the PRINTENV command to display the environment variables for the current shell process.
Local variables
exists only in the current shell process, with the SET command to display all variables defined in the current shell process, including local variables
and environment variables) and functions.
Environment variables are concepts that are present in any process, and local variables are shell-specific concepts. In the shell, the environment variables and the
The definition and usage of the ground variable are similar. Define or assign a variable to a shell:
Varname=value
Note that there can be no spaces on either side of the equal sign, or it will be interpreted by the shell as command and command line arguments. A variable is defined only after
exists in the current shell process, it is a local variable, the export command can be exported to the local variables as environment variables, defining and exporting environment variables can usually be completed in one step:
Export Varname=value
It can also be done in two steps:
Varname=value
Export VARNAME
You can use the unset command to delete a defined environment variable or local variable.
Four. Variable reference
If a variable is called VARNAME, use ${varname} to represent its value, and without causing ambiguity
You can use $varname to represent its value. The differences between the two representations are compared by the following example:
Echo $SHELL
Echo $SHELLABC
Contrast:
Echo ${shell}
Echo ${SHELL}ABC
Note that when you define a variable, you do not use $ when you take the variable value. Unlike the C language, shell variables do not need to be clearly defined
Type, in fact the value of the shell variable is a string, such as we define VAR=45, in fact, the value of Var is the string 45 rather than
Integer The shell variable does not need to be defined before it is used, and the value is an empty string if a variable is not defined.
Five. File name Substitution (Globbing)::*? []
The characters used for matching are called wildcards (Wildcard), as follows:
Wildcard *: matches 0 or more arbitrary characters
? : Matches an arbitrary character
[Several characters]: matches one occurrence of any character in square brackets
$ ls/dev/ttys*
$ ls ch0?. Doc
$ ls Ch0[0-2].doc
$ ls Ch[012][0-9].doc
Note that the file name that the globbing matches is expanded by the shell, which means that the parameters have been expanded before they are passed to the program.
, such as the above LS ch0[012].doc command, if the current directory has Ch00.doc and Ch02.doc, then the parameters passed to the LS command
This is actually the two file name, not a matching string.
Six. Single quotation marks
Unlike the C language, the single and double quotation marks in shell scripts are the same as the delimiter of the string (described in the next section of the double quotation marks),
Instead of the character's delimiter. Single quotation marks are used to hold the literal value of all characters within the quotation marks, even if the \ and carriage returns in quotation marks are not
exceptions, but single quotes cannot appear in the string. If the quotation marks are not paired, enter a carriage return, and the shell gives a continuation prompt.
Ask the user to match the quotation marks to the pair. For example:
650) this.width=650; "title=" 4.png "src=" http://s3.51cto.com/wyfs02/M01/86/2F/wKiom1e2_ Chahgrwaaarpxft1di549.png-wh_500x0-wm_3-wmp_4-s_2548661550.png "alt=" wkiom1e2_chahgrwaaarpxft1di549.png-wh_50 "/ >
Seven. Double quotes
Double quotation marks are used to keep the literal value of all characters in quotation marks (carriage return is no exception), except in the following cases:
$ plus variable name to take the value of the variable
Anti-quotation marks still indicate command substitution
\$ represents the literal value of $
The literal value of \ ' representation '
The literal value of \ "represents"
\ \ denotes the literal value of \ In addition to the above, there is no special meaning in front of other characters, only the literal value:
Getting Started with shell scripts-symbol Chapter