In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Linux Shell wildcard, metacharacter, escape character example usage", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of Xiaobian slowly in-depth, together to study and learn "Linux Shell wildcard, metacharacter, escape character example usage"!
Here is an example:
[chengmo@localhost ~/shell]$ ls a.txt b.txt c.old #2 [chengmo@localhost ~/shell]$ ls *.txt a.txt b.txt #3 [chengmo@localhost ~/shell]$ ls d*.txt ls: Unable to access d*.txt: There is no file or directory
From the above example, I don't know if you have found any problems. Let's first understand the wildcard knowledge, and then analyze this example.
Linux shell wildcard
Wildcards are handled by the shell (not by the command statement involved, and we don't find them in shell commands), and they only appear in the "parameters" of the command (they don't have to be in the command name or in the operator). When the shell encounters a wildcard in Parameters, it uses it as a path or filename to search disk for a possible match: if a matching match exists, substitution (path extension) is performed; otherwise, the wildcard is passed as a normal character to Command, which processes it. In short, wildcards are essentially a shell implementation of path extension functionality. After the wildcard is processed, the shell completes the reorganization of the command, and then continues processing the reorganized command until the command is executed.
Let's go back to the above command: in the second command, *.txt actually shell search file, found the file that meets the conditions, the command will become: ls a.txt b.txt , actually passed to it when executing ls is a.txt b.txt .
Command 3, d*.txt Since there is no such file or directory under the current directory, directly pass "d*.txt" as ls parameter to ls . At this point "*" is just an ordinary ls parameter and has lost its wildcard meaning. Because the file could not be found, a: Unable to access prompt appears!
Understanding shell wildcards, we now look at the shell common wildcards there are some.
Shell Common Wildcards:
character meaning instance * matches 0 or more characters a*b Any character of any length can be between a and b, or none at all, e.g. aabcb, axyzb, a012b, ab.? Match any character a? b There must be only one character between a and b, and it can be any character, such as aab, abb, acb, a0b. [list] matches any single character in the list a[xyz]b There must be only one character between a and b, but only x or y or z, such as axb, ayb, azb. [! list] matches any single character except a [! 0-9]b There must also be only one character between a and b, but it cannot be an Arabic numeral, such as axb, aab, a-b. [c1-c2] matches any single character in c1-c2 such as: [0-9] [a-z]a[0-9]b There must also be only one character between 0 and 9 such as a0b, a1b... a9b。{string1,string2,...} Match sring1 or string2 (or more) with one of the strings a{abc,xyz,123}b. Between a and b, there must be one of the three strings abc or xyz or 123.
One caveat: Wildcards look a bit like regular expression statements, but they are different from regular expressions and should not be confused with each other. Think of wildcards as shell special code characters. And it's only about * [] ,{} these kinds.
shell metacharacters (special character Meta)
In addition to wildcards, the shell is responsible for parsing in advance and passing the processing results to the command line. The shell also has a series of other special characters of its own.
The character description IFS consists of or or one of the three (we often use space ). CR is generated by.= Set variables.$ Make variable or arithmetic substitutions (not to be confused with shell prompt).> Redirect to stdout. * space tab
control operator
A token that performs a control function. It is one of the following symbols:
|| & && ; ;; ( ) |
III. Shell escape characters
Sometimes, we want wildcards, or metacharacters, to be normal characters, so we don't need to use them. So we need to use escape characters here. Shell provides three escape characters.
The character description ''(single quotes) is also called hard escape, and all shell metacharacters and wildcards inside it are turned off. Note that '(single quotes) is not allowed in hard escapes. ""(double quotation marks) is also called soft escape, which only allows certain shell metacharacters to appear inside: $is used for parameter substitution `for command instead of\(backslash), which is also called escape, removing the special meaning of metacharacters or wildcards immediately after it.
Man bash is explained as follows:
There are three quoting mechanisms: the escape character, single quotes, and double quotes.
Examples:
[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 escape character "*" has lost its wildcard meaning.
The process of shell parsing scripts
See the above said these, presumably everyone will ask this question is, there are special characters like this, wildcards, then the shell gets a command, how to deal with it? Let's look at the following chart:
If enclosed in double quotes, shell detection skips steps 1-4 and 9-10, and enclosed in single quotes, shell detection skips steps 1-10. That is, double quotes are sent to the execution step only through parameter extension, command substitution, and arithmetic substitution, while single quotes escape characters are sent directly to the execution step. Moreover, both double quotes and single quotes can tell that the command itself is integrated when executed, but it is not part of the command's Chinese text when executed.
Thank you for reading, the above is "Linux Shell wildcard, metacharacter, escape character instance usage" content, after the study of this article, I believe that everyone on Linux Shell wildcard, metacharacter, escape character instance usage this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.