Linux Shell wildcards, escape characters, metacharacters, special characters

Source: Internet
Author: User

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 character in parameters, the shell treats it as a path or file name to search for possible matches on the disk: replace (path extension) if a matching match exists, or the wildcard is passed as a normal character to "command" and 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.

For example, the following command:
[[email protected] wildcard]# lsa.htm  b.htm[[email protected] wildcard]# ls *.htma.htm  b.htm[[email protected] wildcard]# ls d*.htmls:d*.htm: no file or directory

The analysis is as follows:

The 2nd command, for the parameter *.htm,shell in the current directory can search for matching files, will be replaced by a.htm b.htm. The actual execution is the LS a.htm b.htm command.

The 3rd command, for the parameter d*.htm, the shell in the current directory can not find a matching file, then directly d*.htm as the LS parameter, the actual execution is the LS d*.htm. The file cannot be found, so it appears: there is no file or directory.

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

Shell wildcard characters
character meaning Example
* Match 0 or more characters A*b A and B can have any character of any length, or there can be no one, such as AABCB, a01b, AB, etc.
? Match any one character A?b A and B have only one character, which can be any character, such as AAB, ADB, a0b, etc.
[List] Match any single character in the list A[xyz]b A and B have only one character and can only be 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 have only one character, but cannot be a number, such as AXB, AAB, A-a, etc.
[C1-C2] Match any single character in the C1-C2 There is only one character between A[0-9]b A and B, and the character is a number between 0-9, 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.


second, Shell meta-characters

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.

Shell Meta characters
character Description
= Variable name = value, assigning a value to the variable. Note = around the variable name and value, do not have spaces in the middle
$ Variable value substitution, $ variable name replaced with the value of the shell variable, to avoid confusion when text joins, use the ${variable name};$0...$9 to represent the parameters of the shell file.
> Prog > file redirects the standard output to files.
>> Prog >> file appends the standard output to files.
< Prog < file get the standard input from files
| Pipe command, example: P1 | P2 the standard output of the P1 as a standard input for P2
& The biggest benefit of running commands in the background is that you can continue to enter commands under the same command line without waiting for the command to finish
() Executing commands in a child shell
{} Executes the command in the current shell, or uses the defined scope of the variable substitution (for example, the ${variable name} usage above).
; The command terminator. For example, P1;P2 indicates that P1 is executed first and then P2
&& The previous command executes successfully before the next command is resumed. Example: P1 && p2, if P1 execution succeeds, the P2 is executed, otherwise, p2 is not executed;
|| The previous command failed to continue execution of the next command. Example: P1 | | P2, if P1 execution succeeds, do not execute P2, vice versa, only execute P2;
! Commands in the execution history
~ Home Directory



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

Shell Escape character
character Description
‘‘ Single quotes, hard escapes, and all shell metacharacters and wildcards inside them are turned off. Note that ' (single quotation marks) are not allowed in hard escaping.
"" Double quotes, soft escapes, which are only allowed to appear within the specific shell metacharacters ($, ', \): $ for Variable value substitution, ' for command substitution, \ \ For escaping a single character
\ Backslash, escaping, removes the special meaning of the meta-character or wildcard immediately followed.

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