Linux shell wildcard characters, metacharacters, escape characters

Source: Internet
Author: User


Introduction to Linux Shell wildcard, metacharacters, escape character usages


When it comes to shell wildcard characters (wildcard), they are often used when used. Here is an example:


[[email protected] ~/shell]$ ls a.txt  b.txt  c.old   #2

[[email protected] ~/shell]$ ls *.txt a.txt  b.txt   #3

[[email protected] ~/shell]$ ls d*.txt ls: Cannot access d *. TXT: there is 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 relevant knowledge of the wildcard, and then analyze the example.



One, the Linux shell wildcard character (wildcard)



Wildcards are handled by the shell (not by the command statements involved, but we are not aware of these wildcard characters in the shell commands), it will only appear in the "Parameters" of the command (it is not in the command name, nor on the operator). When the shell encounters a wildcard in parameters, the shell treats it as a path or file name to search for possible matches on the disk: If a matching match exists, the substitution (path extension) is performed, otherwise the wildcard is passed as a normal character to the command, which is then processed by the command. In short, a wildcard is actually a kind of path extension that the shell implements. After the wildcard is processed, the shell completes the reorganization of the command before continuing with the reorganized command until the command is executed.






We go back to the above command: in the 2nd command, *.txt the actual shell search file, found the matching file, the command will become: LS a.txt b.txt, actually in the execution of LS when the A.txt b.txt.






and command 3,d*.txt because there is no such file or directory under the current directory, the "D*.txt" as the LS parameter, passed to LS. This time "*" is just a common LS parameter, has lost its wildcard meaning. Because the file cannot be found, it will appear: Unable to access the prompt!



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



Shell Common wildcard characters:


Character Meaning Instance
* Match 0 or more characters A*b A and B can have any character of any length, or there can be none, such as AABCB, AXYZB, a012b, AB.
? Match any one character A?b A and B must also 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 also have only one character, but only x or Y or Z, such as: AXB, Ayb, Azb.
[!list] Matches any single character except the list A[!0-9]b A and B must also have only one character, but not Arabic numerals, such as AXB, AAB, a-a.
[C1-C2] Match any single word in c1-c2 such as: [0-9] [A-z] A[0-9]b 0 and 9 must also have only one word such as a0b, a1b ... a9b.
{String1,string2,...} Match sring1 or string2 (or more) one string A{abc,xyz,123}b A and B can only be one of the three strings of ABC or XYZ or 123.





It is necessary to note that the wildcard looks a bit like a regular expression statement, but unlike regular expressions, it cannot be confused with each other. The wildcard character can be interpreted as a shell special code. And the only thing involved, *,? [], {} these kinds.









Second, Shell metacharacters (special character Meta)



In addition to the wildcard character, the shell is responsible for pre-parsing the processing results to the command line, and the Shell has a series of its own other special characters.


Character Description
Ifs Made up of <space> or <tab> or <enter> (we use space).
CR Generated by <enter>.
= Set the variable.
$ Make a variable or operation substitution (please do not confuse the shell prompt).
> Re-directs the stdout. *
< Re-directs the stdin. *
| Command pipeline. *
& Redirect the file descriptor, or place the command in the back-border execution. *
( ) Place the commands inside nested subshell, or for operation or command substitution. *
{ } Place the commands inside the non-named function, or use the defined scope of the variable substitution.
; Proceed to the next command at the end of the previous command, ignoring its return value. *
&& 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. *
! Executes the commands in the History list. *


Adding "*" is a function of the command name directly. You can see the shell metacharacters, basically acting on the command, as a multi-command split (or parameter splitting). As a result, you see the same characters as wildcards, but they are actually scoped differently. So there is no confusion.






The following is the English parsing that man bash has received:



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>






Third, Shell escape character



Sometimes, we want to make a wildcard, or meta-character, into a normal character, without needing to use it. So here we need to use the escape character. There are three types of shell-provided escape characters.


Character Description
"(single quote) Also called hard escape, all of the shell metacharacters and wildcards inside it are turned off. Note that ' (single quotation marks) are not allowed in hard escaping.
"" (double quotes) Also called soft escape, which allows only specific shell metacharacters to appear inside: $ for parameter substitution ' for command substitution
\ (back slash) Also called escape, removing the special meaning of the meta-character or wildcard immediately followed.


The man bash is explained in English as follows:



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



Instance:

[[email protected] ~ / shell] $LS \ *. Txtls: unable to access *. TXT: no file or directory 

[[email protected] ~ / shell] $ls' *. TXT 'LS: unable to access *. TXT: no file or directory [[email protected] ~ / shell] $ls' a.txt' a.txt 

[[email protected] ~ / shell] $LS *. Txta.txt b.txt



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



Iv. process of shell parsing scripts


See above said, presumably everyone will ask this question is, have so think special character, wildcard, then the shell gets a command, arrive how to deal with it? Let's look at the following diagram:


If enclosed 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. In other words, the double quotation marks can be fed into the execution step only by parameter extension, command substitution and arithmetic substitution, and the single-quote escape character is fed directly into the execution step. Moreover, both the double-quote escape character and the single-quote escape character can tell the individual commands to be one within themselves when executed, but they are not part of the text in the command at the time of execution.


Linux shell wildcard characters, metacharacters, escape characters


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.