Introduction to Linux Shell wildcards, metacharacters, and escape characters

Source: Internet
Author: User

When it comes to shell wildcard (wildcard), it is often used. The following is an example:

#1
[chengmo@localhost ~/shell]$ lsa.txt  b.txt  c.old#2
[chengmo@localhost ~/shell]$ ls *.txta.txt  b.txt#3
[Chengmo @ localhost ~ /Shell] $ ls d *. txtls: cannot access d *. txt: No file or directory

From the above example, I wonder if you have found any problems. Let's take a look at the wildcard-related knowledge, and then analyze this instance.

1. linux shell wildcard (wildcard)

Wildcards are processed by shell (not by the command statements involved, but we have not found any introduction to these wildcards in shell commands ), it only appears in the "parameter" of the command (it does not need to be in the command name or operator ). When shell encounters a wildcard in the "parameter", shell will regard it as a path or file name to search for possible matching on the disk: If the matching meets the requirements, then perform replacement (Path Extension). Otherwise, the wildcard is passed as a common character to the "command" and then processed by the command. In short, wildcards are actually a shell-implemented Path Extension function. After a wildcard is processed, shell reassembles the command and then processes the restructured command until the command is executed.

 

Let's go back and analyze the above command: in the 2nd commands ,*. txt: the actual shell searches for the file and finds the file that meets the conditions. The command will be changed to: ls a.txt B .txt. The a.txt B .txt file is actually passed to the directory when the ls is executed.

 

Command 3, d *. txt because there is no such file or directory under the current directory, directly convert "d *. txt is passed to ls as the ls parameter. at this time, "*" is just a common ls parameter and has lost its configuration significance. Because the file cannot be found, the following error occurs: inaccessible!

After learning about shell wildcards, let's take a look at some common shell wildcards.

Common shell wildcards:

Character Description Instance
* Matches 0 or multiple characters A * B a and B can contain any character of any length or none, such as aabcb, axyzb, a012b, and AB.
? Match any character A? B a and B must have only one character, which can be any character, such as aab, abb, acb, a0b.
[List] Match any single character in the list A [xyz] B a and B must have only one character, but only x, y, or z, such as axb, ayb, and azb.
[! List] Match any single character except list A [! 0-9] B a and B must have only one character, but cannot be Arabic numerals, such as axb, aab, a-B.
C1-c2 Match any single character in the c1-c2 such as: [0-9] [a-z] A [0-9] B 0 and 9 must have only one character, such as a0b, a1b... a9b.
{String1, string2 ,...} Match one of sring1 or string2 (or more) strings A {abc, xyz, 123} B a and B can only be one of the three strings abc, xyz, or 123.

 

It should be noted that the wildcard looks a bit like a regular expression statement, but it is different from a regular expression and cannot be confused with each other. The wildcard character can be understood as a special shell code character. And only ,*,? .

 

 

2. shell metacharacters (Special Character Meta)

In addition to wildcards, shell is responsible for pre-parsing and passing the processing result to the command line. shell also has a series of its own special characters.

Character Description
IFS It is composed of <space>, <tab>, or <enter> (commonly used space ).
CR Generated by <enter>.
= Set variables.
$ Replace the variable or operation (do not mix it with shell prompt ).
> Rewrite stdout. *
< Rewrite stdin. *
| Command pipeline. *
& Rewrite the file descriptor or place the command in the background for execution. *
() Place the commands in it in the nested subshell for execution, or use it for calculation or command replacement. *
{} Place the commands in the non-named function for execution, or use the commands in the defined range of variable replacement.
; When the previous command ends, ignore the returned value and continue to execute the next command. *
&& If the return value is true at the end of the previous command, continue to run the next command. *
| When the previous command ends, if the returned value is false, continue to run the next command. *
! Run the commands in the history list. *

"*" Is used directly in the command name. The shell metacharacters are used to separate multiple commands (or separate parameters ). Therefore, we can see that there are the same characters as the wildcard, but they actually have different scopes. So there will be no confusion.

 

The following is an English explanation obtained by man bash:

Metacharacter
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:
| &; () | <Newline>

 

3. shell escape characters

Sometimes, you do not need to use wildcards or metacharacters to convert them into common characters. Here we need to use escape characters. Shell provides three escape characters.

Character Description
''(Single quotes) It is also called a hard escape. All shell metacharacters and wildcards in it will be disabled. Note: '(single quotation marks) is not allowed in hard escape ).
"" (Double quotation marks) It is also called soft escape, which only allows specific shell metacharacters: $ for parameter substitution 'for command substitution
\ (Backslash) It is also called escape, which removes the special meaning of the followed metacharacters or wildcards.

Man bash is interpreted as follows:

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

Instance:

 

[Chengmo @ localhost ~ /Shell] $ ls \ *. txtls: cannot access *. txt: No file or directory [chengmo @ localhost ~ /Shell] $ ls '*. txt' ls: cannot access *. txt: No file or directory [chengmo @ localhost ~ /Shell] $ ls 'a.txt'a.txt [chengmo @ localhost ~ /Shell] $ ls * .txta.txt B .txt

 

As you can see, the added Escape Character "*" has lost the meaning of the wildcard.

4. shell parsing script Process

If you see the above, you may ask this question: if you think about special characters and wildcards, how does the shell handle the arrival of a command? Let's take a look at the following figure:

If double quotation marks are used, shell detection skips steps 1-4 and 9-10. If single quotation marks are used, shell detection skips steps 1-10. That is to say, double quotation marks can be sent to the execution step only after parameter extension, command replacement, and arithmetic replacement, and the single quotation mark escape character is directly sent to the execution step. In addition, both double quotation marks and single quotation marks can tell each command itself to be integrated during execution, but they are not part of the text in the command.

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: info-contact@alibabacloud.com 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.