Linux Shell wildcards, escape characters, metacharacters, special characters, shell escape

Source: Internet
Author: User

Linux Shell wildcards, escape characters, metacharacters, special characters, shell escape
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, 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.

For example, the following command:
[Root @ localhost wildcard] # lsa.htm B .htm [root @ localhost wildcard] # ls * .htma.htm B .htm [root @ localhost wildcard] # ls d *. htmls: d *. htm: the file or directory does not exist.

The analysis is as follows:

In the 2nd commands, for the * .htm parameter, shellcan search for the matching file in the current directory and convert it into a.htm B .htm. The actual execution is the ls a.htm B .htm command.

3rd commands, for the parameter d *. if no matching file is found in the current directory of htm and shell, directly change d *. htm is used as the ls parameter. The actual execution is ls d *. htm. Because the file cannot be found, the file or directory does not exist.

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

Shell wildcard
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, a01b, and AB.
? Match any character A? There is only one character between B a and B, which can be any character, such as aab, adb, a0b, etc.
[List] Match any single character in the list A [xyz] B a has only one character between B and can only be x, y, or z, such as axb, ayb, and azb.
[! List] Match any single character except list A [! 0-9] B a and B have only one character, but cannot be numbers, such as axb, aab, and a-B.
C1-c2 Match any single character in c1-c2 A [0-9] B a and B have only one character, which is a number between 0-9, 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.

2. shell metacharacters

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.

Shell metacharacters
Character Description
= Variable name = value, assign a value to the variable. Note = keep the variable name and value right and right, with no spaces in the middle
$ Replace the variable value. $ Replace the variable name with the shell variable value. To avoid confusion during text connection, use $ {variable name}; $0... $9 represents the parameters of the shell file.
> Prog> file redirects the standard output to the file.
> Prog> file: append the standard output to the file.
< Prog <file get standard input from file
| Pipeline command, for example, p1 | p2: The standard output of p1 is used as the standard input of p2.
& The biggest advantage of running commands in the background is that you can continue to enter commands in the same command line without waiting for the command to finish running.
() Execute commands in the sub-shell
{} Execute commands in the current shell or use them to define the scope of variable replacement (for example, the above $ {variable name} usage ).
; Command Terminator. For example, p1; p2 indicates that p1 is executed before p2.
&& After the previous command is successfully executed, continue to execute the next command. For example, p1 & p2; If p1 is successfully executed, p2 is executed. Otherwise, p2 is not executed;
| If the previous command fails to be executed, continue to run the next command. For example, p1 | p2. If p1 is successfully executed, p2 is not executed. Otherwise, p2 is executed;
! Execute commands in the history
~ Home Directory

3. shell escape characters sometimes we want to convert wildcards or metacharacters into common characters. Here we need to use escape characters. Shell provides three escape characters.

Shell escape characters
Character Description
'' Single quotation marks and hard escaping. All shell metacharacters and Wildcards are closed. Note: '(single quotation marks) is not allowed in hard escape ).
"" Double quotation marks, soft escape. Only the specific shell metacharacters ($, ', \) are allowed internally: $ is used to replace variable values,' is used to replace commands, and \ is used to escape a single character.
\ Backslash, escape, remove the special meaning of the followed metacharacters or wildcards.

How to distinguish between * And metacharacters in linux *

Escape Character \

Linux shell sed commands and escape characters

You can change the delimiters without escaping them.
For example:
A = "sed 3/06/09" sed "s # hello # $ #"
Change the separator.

Thank you for your support!

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.