ShellYesuser andLinuxThe interface between operating systems. There are multiple shells in Linux, where Bash is used by default. This chapter describes how shell works, shell types, General shell operations, and Bash features.
Special characters in shell
In shell, in addition to common characters, you can also use special characters with special meanings and functions. When using them, pay attention to their special meanings and scopes. The following describes these special characters.
1. wildcard
Wildcard characters are used for pattern matching, such as file name matching, path name search, and string search. Common wildcard characters include *,? And the Character Sequence enclosed in square brackets. You can include These wildcards in the file name of the command parameter to form a so-called "pattern string" and perform pattern matching during execution.
* Represents the length of any string.) For example, "f *" matches any string that starts with "f. Note that the dot.) before the file name must be explicitly matched with the slash/) in the path name. For example, "*" cannot match. file, but ". *" can match. file.
? Represents any single character.
[] Indicates a specified character range. As long as the character at the [] position in the file name is within the range specified by [], the file name matches the pattern string. The character range in square brackets can be composed of directly given characters, or it can be composed of starting characters, ending characters, and intermediate hyphen-) that indicate a limited range. For example, f [a-d] and f [abcd] serve the same purpose. Shell uses all file names that match the pattern string specified in the command line as command parameters to form the final command and then execute the command.
Table 10-1 describes the meanings of these wildcards.
Table 10-1 wildcard meanings
Mode string
Yi
*
The names of all files in the current directory.
* Text *
The names of all files with Text in the current directory.
[AB-dm] *
Names of all files starting with a, B, c, d, and m in the current directory.
[AB-dm]?
The names of all files starting with a, B, c, d, and m in the current directory, followed by only one character.
/Usr/bin /??
The names of all files with two characters under the/usr/bin directory.
Note that the hyphen "-" is only valid in square brackets and indicates the character range. For example, it becomes a common character outside square brackets. And * and? Wildcards are used only outside square brackets. If they appear in square brackets, they also lose the wildcard capability and become common characters. For example, the mode "-a [*?] Only one pair of square brackets in abc is a wildcard, * and? Are common characters. Therefore, the matching strings can only be-a * abc and-? Abc. ,
Finally, we will explain some issues that need to be paid attention to when using wildcards. Because *,? And [] are of special significance for shell, so these characters should not appear in normal file names. In particular, do not show them in the directory name; otherwise, the Shell matching may be infinitely recursive. In addition, if the directory does not contain a file name that matches the specified mode string, Shell will use this mode string as a parameter to send it to related commands. This may be the reason for the special characters in the command.
2. quotation marks
In shell, there are three types of quotation marks: single quotation marks, double quotation marks, and reverse quotation marks.
* Single quotes'
All characters enclosed by single quotes are common characters. When special characters are enclosed in single quotes, they will also lose their original meaning and will only be interpreted as common characters. For example:
- $ string=’$PATH’
-
- $ echo $string
-
- $PATH
-
- $
It can be seen that $ retains its meaning and appears as a common character.
* Double quotation marks
Characters enclosed by double quotation marks, except the characters $, ', and' are still special characters and keep their special functions. Other characters are still treated as common characters. For $, it is to replace this variable and $ with the variable value specified later. For, it is an escape character, it tells shell not to perform special processing on the character after it, just as a common character. You can imagine that the double quotation marks must be preceded by only four characters $, ', and. If no signature is added before the signature, Shell matches the signature with the previous signature.
For example, assume that the PATH value is.:/usr/bin:/bin and enter the following command:
- $ TestString=”$PATH”$PATH”
-
- $ echo $TestString
-
- .:/usr/bin:/ bin”$PATH
-
- $
You can try the result by yourself without adding the second double quotation mark.
* Reverse quotation marks
The key corresponding to this character is generally located in the upper left corner of the keyboard. Do not confuse it with single quotation marks. The string enclosed by backquotes is interpreted by shell as a command line. During execution, shell first executes the command line and replaces the whole backquotes with its standard output results). For example:
- $ pwd
-
- /home/xyz
-
- $ string=”current directory is `pwd`”
-
- $ echo $string
-
- current directour is /home/xyz
-
- $
When shell executes the echo command, it first executes the command pwd in 'pwd', replaces the output result/home/xyz with the 'pwd', and finally outputs the entire replaced result.
This function can be used to replace commands. That is, the execution result enclosed by backquotes is assigned to the specified variable. For example:
- $ today=`date`
-
- $ echo Today is $today
-
- Today is Mon Apr 15 16:20:13 CST 1999
-
- $
Backquotes can also be nested. However, you must note that the inner anti-quotation marks must be used as backslashes to escape nested expressions. For example:
- $ abc=`echo The number of users is `who| wc-l``
-
- $ echo $abc
-
- The number of users is 5
-
- $
Shell special characters can also be used in the command line between backquotes. Shell is used to get the result of the command in ''. It actually needs to execute the command specified in. Special characters in the command, such as $ ,",? And can contain any legal Shell command, such:
- $ ls
-
- note readme.txt Notice Unix.dir
-
- $ TestString=”`echo $HOME ` ` ls [nN]*`”
-
- $ echo $TestString
-
- /home/yxz note Notice
-
- $
In other cases, you can try it yourself.