Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of the use of Shell wildcards, metacharacters and escape characters in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "summary of the use of Shell wildcards, metacharacters and escape characters in Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

When it comes to shell wildcards (wildcard), people will often use them. Here is an example:

[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: cannot access d*.txt: there is no such file or directory

From the above example, I don't know if you have found any problems. Let's first understand the knowledge of wildcards, and then analyze this example.

I. linux shell wildcard (wildcard)

Wildcards are handled by shell (not by the command statements involved, which we don't find in the various shell commands), but only in the "parameters" of the command (it is not used in the command name or on the operator). When shell encounters a wildcard in parameters, shell uses it as a path or file name to search for possible matches on disk: if a match exists, it is replaced (path extension); otherwise, the wildcard is passed to the command as a normal character, and then processed by the command. In short, wildcards are actually a kind of path extension implemented by shell. After the wildcard is processed, shell first completes the reorganization of the command, and then continues to process the reorganized command until the command is executed.

Let's go back to the above command: in the second command, * .txt actually shell the file and find the file that meets the criteria. The command will become: ls a.txt b.txt, which is actually passed to it when ls is executed.

Because there is no such file or directory under the current directory, the command 3 d*.txt dwells. Txt directly passes "d*.txt" as a ls parameter to ls. At this time, "*" is just an ordinary ls parameter, and it has lost its wildcard meaning. The prompt cannot be accessed because the file cannot be found.

Now that we know about shell wildcards, let's take a look at some of the common shell wildcards.

Common shell wildcards:

It is important to note that wildcards look a bit like regular expression statements, but they are different from regular expressions and should not be confused with each other. Just think of wildcards as shell special code characters. And the only ones involved are *,? [], {}.

2. Shell metacharacter (special character Meta)

In addition to wildcards in shell, where shell is responsible for parsing in advance and passing the processing results to the command line, shell has a series of other special characters of its own.

The addition of "*" is used directly in the command name. You can see that shell metacharacters, basically acting on commands, are used for multi-command segmentation (or parameter segmentation). So you can see that there are the same characters as wildcards, but the scope is actually different. So there will be no confusion.

The following is the English analysis 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:

| | & &; () |

3. Shell escape character

Sometimes we want to turn wildcards, or metacharacters, into normal characters, and we don't need to use it. Then we need to use escape characters here. There are three kinds of escape characters provided by shell.

Man bash is explained as follows:

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

Example:

[chengmo@localhost ~ / shell] $ls * .txt ls: unable to access * .txt: without that file or directory [chengmo@localhost ~ / shell] $ls'* .txt'ls: unable to access * .txt: without that file or directory [chengmo@localhost ~ / shell] $ls' a.txt' a.txt [chengmo@localhost ~ / shell] $ls * .txt a.txt b.txt

As you can see, the addition of the escape character "*" has lost the meaning of the wildcard.

IV. The process of shell parsing script

Seeing what has been said above, you must have asked the question is, if you think of special characters and wildcards so much, how does shell deal with arrival when it gets a command? Let's take a look at the following picture:

If you enclose the double quotation marks, the shell test skips steps 1-4 and 9-10, and if the single quotation marks are included, the shell test skips 1-10 steps. In other words, double quotation marks can be sent to the execution step only through parameter extension, command substitution, and arithmetic substitution, while single quotation mark escape characters are sent directly to the execution step. Moreover, whether it is a double quote escape character or a single quote escape character, it can tell each command that it is integrated within itself, but it is not part of the text of the command at the time of execution.

This is the end of the summary of how to use Shell wildcards, metacharacters and escape characters in Linux. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report