Linux Shell wildcard characters, metacharacters, escape character usages introduction

Source: Internet
Author: User
Tags command line regular expression linux

Here is an example:

The code is as follows:

[Chengmo@localhost ~/shell]$ ls

A.txt B.txt C.old


[Chengmo@localhost ~/shell]$ ls *.txt

A.txt B.txt


[Chengmo@localhost ~/shell]$ ls d*.txt

LS: Unable to access d*.txt: No file or directory

From the above example, I do not know whether we have found the problem. Let's take a look at the wildcard related knowledge, and then analyze this example.

Linux Shell wildcard character (wildcard)

Wildcard characters are handled by the shell (not by the command statements involved, in fact, we don't see any of these wildcard descriptions in the shell commands, and it only appears in the command's "arguments" (it doesn't have to be in the command name or on the operator). When the shell encounters wildcard characters in parameters, the shell will use it as a path or filename to search for possible matches on the disk: if matching the required match exists, make a substitution (path extension), otherwise the wildcard will be passed as a normal character to the command and then processed by the command. In short, the wildcard character is actually a shell implementation of the path extension function. After the wildcard is processed, the shell completes the reorganization of the command before proceeding with the reorganized command until the command is executed.

Let's go back and analyze the above command: in the 2nd command, *.txt the actual shell search file, found the eligible file, the command will become: LS a.txt b.txt, actually in the execution of LS time to pass it is a.txt b.txt.

and command 3,d*.txt because there is no such file or directory below the current directory, the "D*.txt" as the LS parameter, passed to LS. This time "*" is just a common LS parameter, has lost its significance. Because the file could not be found, it appears: Unable to access the prompt!

With the shell wildcard, let's look at the common wildcard characters in the shell.

Shell Common wildcard characters:

" meaning instance
matches 0 or more characters a*b A and B can have any length of any character, also can not be one, such as AABCB, AXYZB, a012b, AB.
a?b A and B must also have only one character, can be any character, such as AAB, ABB, ACB, a0b.
[list a[xyz]b A and B must also have only one character, but only x or Y or Z, such as: AXB, Ayb, Azb.
[!list] match except List of any single character a[!0-9]b A and Bmust also have only one character, but not Arabic numerals, such as AXB, AAB, A-b.
[c1-c2 a[0-9]b between 0 and 9 must also have only one word Furu a0b, a1b ... a9b.
a{abc,xyz,123}b A and B can only be one of the three strings of ABC or XYZ or 123.

It should be explained that the wildcard character looks a bit like a regular expression statement, but it is different from regular expressions and cannot be confused with each other. The wildcard is understood as a shell special code character. And the only thing involved, *,? [], {} these several.

Second, Shell metacharacters (special character Meta)

The shell, in addition to the wildcard characters, is responsible for resolving the processing results to the command line, and the Shell has a series of other special characters of its own.

character description
> redirect stdout. *
< redirect stdin. *
| command pipeline. *
& redirect file descriptor, or place commands on the back. *
&& > at the end of the previous command, If the return value is true, proceed to the next command. *
| | at the end of the previous command, if the return value is false, proceed to the next command. *

Adding "*" is a function in the command name directly. You can see the shell metacharacters, basically acting on the command, as a multiple command partition (or parameter segmentation). So you see the same characters as wildcards, but they actually have different scopes. So there will be no confusion.

The following is the English parsing of Man bash:


A character that, when unquoted, separates words. One of the following:

| &; () < > Space tab

Control operator

A token that performs a control function. It is one of the following symbols:

|| & &&;;; ( ) |

Three, Shell escape character

Sometimes we want to make wildcards, or meta characters, into normal characters without using it. So here we need to use the escape character. The shell provides escape characters in three different ways.

Character Description
"(single quote) Also called hard escape, all of its internal shell metacharacters, wildcard characters will be turned off. Note that ' (single quotes) are not allowed in hard escape.
"" (double quotes) Also called soft escape, which allows only specific shell metacharacters to appear: $ for parameter substitution ' for command substitution
(Back slash) Also called escape, removes the special meaning of the metacharacters or wildcards that followed.

Man Bash is explained in English as follows:

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


Copy Code

The code is as follows:

[Chengmo@localhost ~/shell]$ ls *.txt

LS: Unable to access *.txt: No file or directory

[Chengmo@localhost ~/shell]$ ls ' *.txt '

LS: Unable to access *.txt: No file or directory

[Chengmo@localhost ~/shell]$ ls ' a.txt '


[Chengmo@localhost ~/shell]$ ls *.txt

A.txt B.txt

As you can see, adding the Escape character "*" has lost the wildcard meaning.

Iv. the process of shell parsing scripts

See above say these, presumably everybody will ask this question is, have so think special character, wildcard, then shell in get an order, arrive how to deal with? Let's look at the following figure:

If included in double quotes, shell detection skips 1-4 steps and 9-10 steps, and single quotes are included, and shell detection skips 1-10 steps. That is, double quotes can be fed to the execution step only by parameter extension, command substitution, and arithmetic substitution, and the single quote escape character is sent directly to the execution step. Also, whether a double quote escape character or a single quote escape can tell the individual commands that they are internal when executed, it is not part of the text in the command itself.

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: 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.