Linux Shell wildcards, escape characters, metacharacters, special characters

1. Linux shell wildcard (wildcard)

Wildcards are processed by the shell (not processed by the command statements involved, in fact, we have not found these wildcards introduced in various shell commands), it only appears in the "parameters" of the command (it does not need to be in the command name. , and not on the operator). When the shell encounters a wildcard in "arguments", the shell treats it as a path or filename to search for possible matches on disk: if a matching match exists, it is replaced (path expansion); otherwise, the The wildcard is passed to the "command" as a normal character, which is then processed by the command.

In short, wildcards are actually a path expansion function implemented by the shell. After the wildcard is processed, the shell will first complete the reorganization of the command, and then continue to process the reorganized command until the command is executed.

For example the following command:

[root@localhost wildcard]# ls
a.htm b.htm
[root@localhost wildcard]# ls *.htm
a.htm b.htm
[root@localhost wildcard]# ls d*.htm
ls: d*.htm: does not have that file or directory

analyse as below:

In the second command, for the parameter *.htm, the shell can search for a matching file in the current directory, and it will be replaced by a.htm b.htm. What is actually executed is the ls a.htm b.htm command.

The third command, for the parameter d*.htm, the shell can not find a matching file in the current directory, then directly use d*.htm as the ls parameter, and the actual execution is ls d*.htm. Since the file cannot be found, it appears: No such file or directory.

Knowing the shell wildcards, let's take a look at some of the common wildcards in the shell.

shell wildcard  

Examples of character meanings
 *  matches 0 or more characters  a*b a and b can have any characters of any length, or none at all, such as aabcb, a01b, ab, etc.
 ?  matches any character  a?b There is only one character between a and b, which can be any character, such as aab, adb, a0b, etc.
 [list]  matches any single character in list  a[xyz]b There is only one character between a and b, and it can only be x or y or z, such as: axb, ayb, azb.
 [!list]  matches any single character except list  a[!0-9]b There is one and only one character between a and b, but it cannot be a number, such as axb, aab, ab, etc.
 [c1-c2]  matches any single character in c1-c2  a[0-9]b There is one and only one character between a and b, which is a number between 0-9, such as a0b, a1b, ... , a9b.
 {string1,string2,...}  matches one of sring1 or string2 (or more)  a{abc,xyz,123}b between a and b can only be one of the three strings abc or xyz or 123.

It should be noted 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.

Second, shell metacharacters

In addition to wildcards, the shell is responsible for pre-parsing and passing the processing result to the command line. The shell also has a series of other special characters of its own.

shell metacharacters

character description
 =  variable name = value, assign a value to the variable. Note = Follow the variable name and value right and left without spaces
 $  For variable value substitution, $variable name is replaced with the value of the shell variable; to avoid confusion during text concatenation, use ${variable name};$0...$9 to represent the parameters of the shell file.
 >  prog > file redirects standard output to a file.
 >>  prog >> file appends standard output to a file.
 <  prog < file get standard input from file file
 |  Pipeline command, for example: p1 | p2 use p1's standard output as p2's standard input
 &  The biggest advantage of running commands in the background is that you can continue to enter commands under the same command line without waiting for the end of the command execution.
 ()  Execute a command in a subshell
 {}  Execute a command in the current shell, or use a delimited scope in variable substitution (such as the ${variable name} usage above).
 ;  Command terminator. For example, p1;p2 means to execute p1 first, then execute p2
 &&  After the previous command is successfully executed, the next command is executed. Example: p1 && p2 ; if p1 is executed successfully, p2 will be executed, otherwise, p2 will not be executed;
 ||  Continue to execute the next command only after the execution of the previous command fails. Example: p1 || p2 ; If p1 is executed successfully, p2 will not be executed, otherwise, p2 will be executed;
 !  Execute commands in history
 ~  home directory

Three, shell escape character

Sometimes, we want to make wildcards, or metacharacters, into ordinary characters. So here we need to use the escape character. There are three types of escape characters provided by the shell.

shell escape character

character description
 ''  Single quotes, hard escapes, and all shell metacharacters and wildcards inside them will be turned off. Note that ' (single quotes) are not allowed in hard escapes.
 ""  Double quotes, soft escape, only allow specific shell metacharacters ($,`,\) inside them: $ is used for variable value substitution, ` is used for command substitution, \ is used to escape a single character
 \  Backslash, escape, removes the special meaning of the metacharacter or wildcard immediately following it.

Shell programming analysis  http://www.linuxidc.com/Linux/2014-08/105379.htm 

Linux Shell parameter substitution  http://www.linuxidc.com/Linux/2013-06/85356.htm

Shell for parameters  http://www.linuxidc.com/Linux/2013-07/87335.htm

Linux/Unix Shell parameters passed to SQL scripts  http://www.linuxidc.com/Linux/2013-03/80568.htm

Shell脚本中参数传递方法介绍 http://www.linuxidc.com/Linux/2012-08/69155.htm

Shell脚本传递命令行参数 http://www.linuxidc.com/Linux/2012-01/52192.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2014-10/108111.htm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394735&siteId=291194637