Linux Bash's wildcard

Wildcards are we unknowingly will be used in the shell environment, and sometimes not even consider going to look into the implementation process, since too common. And a clear understanding of each process will help our analysis and debugging.

To put it plainly, is the wildcard in the  shell  is used to select a given pattern matching special characters in file cluster environment. Here is a relatively formal definition, quoted from the https://bash.cyberciti.biz/guide/Wildcards .

Wildcards is one of the most important features of Bash shell. It allows you to select a group of files. For example you can select all C programming files in
a GUI file manager with mouse. To select all C programming files in a Bash shell you use wildcards.
In short wildcards are nothing but special characters that allows you to select a group of files that matches certain pattern.

Wildcards are generally only in the parameters of the command, when the  shell  encountered a wildcard parameter, it's treated like a file name or path most likely match the search area on the disk, if they meet the requirements of a match, then perform substitution, otherwise, the wildcard is passed as an ordinary character to the command, and then processed by the command.

Shown in Figure 1-1 is a  shell  whole process when parsing the command line parameters, but it is important to very cumbersome.

shell command line parameter parsing process

shell  common entries such as shown in Table 1.

Apart wildcards, there are a unique set of special characters element, as shown in Table 2. Metacharacters acting substantially above command, the command as a multi-split or split parameter. Pay attention here,  {}  , because there Table 1 has a similar character, in fact, their scope is not the same, so there is no confusion.

metacharacter     A character that, when unquoted, separates words. One of the following: |  & ; ( ) < > space tab

In addition to Table 1 and Table 2, the special characters, if we want to turn it into an ordinary character, need to use escape character in the shell provides three escape character, as shown in Table 3.

There are three quoting mechanisms: the escape character, single quotes, and double quotes.

A non-quoted backslash (\) is the escape character. It preserves the literal value of the next character that follows, with the exception of <newline>. If a \<newline>
pair appears, and the backslash is not itself quoted, the \<newline> is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).
Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !.
The characters $ and ` retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or <newline>. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes
is escaped using a backslash. The backslash preceding the ! is not removed.

Wherein the difference between single quotes and double quotes as follows:

  • Single quotes are strong references, it ignores all the special treatment being caused to the character, the character will be quoted to be used intact, but then quoted single quotes are not allowed within the range of quotes.
  • Double quotation marks belong to weak reference, it will be quoted several of the characters of special treatment, others are ignored. E.g:
    1. Wherein  $  plus variable name may take the value of the variable;
    2. Anti quotes and  $ ()  brackets characters will be treated as a command to replace the original character after execution;
    3. Need to use  $ ` '  required by a backslash time.

Reference material

[1]  shell script command parsing [process]

[2]  the Linux Shell wildcard character element, escape Example describes

Guess you like

Origin www.cnblogs.com/phillee/p/11831778.html