Reference symbols protect special metacharacters and parameter extensions in the parser using 3 ways: backslash (\), single quotation mark ('), double quotation mark ("").
Single and double quotes must be matched to protect special metacharacters and wildcards in the parser, but single quotation marks (hard escapes) are used primarily for the original output of information, to prevent any expansion of variables, and double quotes (soft escaping) allow variable extensions, escape characters, and command substitutions. Single quotation mark protection double quotation mark, double quotation mark protection single quotation mark.
1. Back slash
A backslash converts 1 characters into reference symbols or escapes through the parser.
1) using a backslash in single quotation marks, the backslash is not parsed and is output as-is.
2) use backslashes in double quotes, mainly for dollar sign ($), anti-single quotation mark ('), double quotation mark (""), single quote (') output, and escape character parsing, where single quotes in the output, backslashes and double quotes cannot be used simultaneously.
3) without the use of single and double quotation marks (e.g. echo \, the output is empty), the backslash can be considered as auto-ignore.
2. Single quotation marks
1) The parsing of any variable, metacharacters, wildcard, escape character is not allowed in single quotation marks, and is output as is.
2) use double or backslash escapes to display the output single quotation marks, but double quotes and backslashes cannot be used together.
such as command: echo "\", the output will be (\ '), instead of (')
3) resolvable regular expression, used in conjunction with SED and grep commands
3. Double quotes
The protection of special meta characters and wildcards is not parsed by the shell, but allows for variable and command substitution, as well as escape character parsing.
4. Appendix
Learn about common Linux special characters, including wildcards, escape characters, and metacharacters.
4.1 Wildcard characters (wildcard)
A wildcard is actually a shell-implemented path extension feature that, when the shell encounters a wildcard in parameters, searches for a possible match on disk as a path or file name, and the following table lists the more commonly used shell wildcard characters for your reference and learning.
Character |
Meaning |
Instance |
* |
Match one or more characters |
A*b A and B can have any length of character |
? |
Match any one character |
There must be only one character between A?b A and B |
[List] |
Match any single character in the list |
A[xyz]b A and B must also have only one character between [XYZ] A[0-9]b A and B must also have only one numeric character between 0-9 There is only one character between A[!0-9]b A and B, and the character cannot be 0-9 |
{String List} |
Matches any string in a list of strings |
Between A{abc,xyz,123}b A and B can only be ABC or XYZ or 123 |
It is necessary to note that wildcards are somewhat similar to regular expression statements, but unlike regular expressions, they can only be interpreted as shell special characters, and only *,?,[] and {} are involved.
4.2 Meta characters (META)
Character |
Description |
; |
Command delimiter, ignoring its return value at the end of the previous command, and continuing to execute the next command |
& |
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 |
= |
Variable name = value, assigning a value to the variable. Note that the "=" immediately follows the variable name and value, with no spaces in the middle |
$ |
Variable value substitution, "$ variable name" is replaced with the "value" of the shell variable, to avoid confusion when text is concatenated, use the ${variable name} |
> |
Output redirection, prog > file redirect standard output to files (overwrite original file contents) |
>> |
Output redirection, prog >> file append standard output to files |
< |
Input redirection, 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 |
() |
Executes a command in a child shell, or for an operation, or for a command substitution ($ (command)) |
{} |
Execute the command in the current shell, or use the scope of the variable substitution (for example, the ${variable name above} usage) |
&& |
The previous command executes successfully before the next command is resumed. Example: P1 && p2, if P1 execution succeeds, execute P2, otherwise, do not execute P2 |
|| |
The previous command failed to continue execution of the next command. Example: P1 | | P2, if the P1 execution succeeds, does not execute P2, vice versa, only then executes P2 |
! |
Commands in the execution history list, such as!1 |
~ |
User home Directory |
4.3 escape character
The shell provides an escape character in 3: Single quotation mark ('), double quotation mark (""), and backslash (\). The escape character has been described in detail in the above, just a brief summary of it.
Character |
Description |
"(single quote) |
Hard escapes, and all shell metacharacters, wildcards, and so on, are not parsed for the original output of the parameters. Note that single quotes are not allowed in hard escape |
"" (double quotes) |
Soft escape, which can be used to protect meta-characters and wildcards, but allows extensions of variables and commands to be replaced, and also allows for parsing of backslash escape characters |
\ (back slash) |
Escaped, protected metacharacters or wildcards are not parsed by the shell, primarily for double quotes, single quotes, inverted single quotes, and dollar signs |
When used with echo and sed, common backslash escape characters are shown in the following table:
Escape character |
Significance |
\a |
Bell character |
\b |
Backward character, that is, delete the previous one |
\c |
Output does not wrap, contrary to/n action |
\d |
Date as "week, month, day" |
\f |
Line breaks, but the cursor remains in place |
\h or \h |
Displays the base hostname or displays the full host name |
\ t or \ t |
24-hour or 12-hour system time |
\ n |
Output line break |
\ r |
The cursor moves to the beginning of the line and does not wrap |
\s |
The name of the current shell, such as bash |
\ t |
Insert Tab key |
\u |
User name of the current login shell |
\v |
Bash version |
\w |
Current working directory |
\w |
Base name of the current working directory |
\\ |
Backslash \ \, with matchless quotes, same result |
\$ |
Dollar sign $, with Matchless quotes, with the same result |
\" |
Double quotes ", with the same result as the matchless quotation marks |
\‘ |
Single quote ', cannot be used in conjunction with double quotes |
\` |
Anti-single quotes ', with matchless quotes, with the same result |
\nnn |
Insert ASCII characters represented by octal |
Daily Linux Command Learning-reference symbol (backslash \, single quote ", double quote")