Home  >  Article  >  Operation and Maintenance  >  Summary of commonly used special characters in bash

Summary of commonly used special characters in bash

巴扎黑
巴扎黑Original
2017-08-17 10:31:381707browse

#Comment symbol (Hashmark)

1. At the beginning of the line of the shell file, as the shell call interpreter mark, #!/bin/bash;

2. Used as a comment in the configuration file. In a line, the content after # will not be executed.

;

is used as the separator for multiple commands (Command separator [semicolon]).

When multiple commands are to be placed on the same line, they can be separated by semicolons.

;;

Continuous semicolon (Terminator [double semicolon]).

When using the case option, it is used as the terminator of each option.

.

Dot command.

1. Execute the current directory file

                #!/bin/bash

      .pythontab-file

2. As part of the file name, in the file The beginning of the name indicates that the file is a hidden file

3. As a directory name, one dot represents the current directory, and two dots represent the upper directory (the parent directory of the current directory). Note that more than two dots do not appear unless you surround the dot character itself with quotation marks (single/double);

4. In regular expressions, the dot represents any character.

"

Double quotation marks.

Partial quotation. Content surrounded by double quotation marks allows variable expansion and the existence of escape characters.

'

Single quotation mark (full quoting [single quote]).

All characters enclosed in single quotation marks are treated as the characters themselves,

Comma (comma operator [comma]).

1. Used to connect a series of mathematical expressions. This series of mathematical expressions are evaluated, but only the last evaluation result is returned. For example: ##.

##!/Bin/Bash

Lett1 = (((A = 5+1, B = 7+2))

## Echot1 = $ t1, a = $ a, b, b, b, b =$b

      ##This $t1=$b;

\

Backslash, backslash (escape [backslash]). 1. Placed before the special symbol, the function of escaping the special symbol only represents the special symbol itself, which is commonly used in strings;

2. Placed at the end of a line of instructions, indicating the subsequent carriage return Invalid (in fact, Enter is escaped), the input of subsequent new lines is still regarded as part of the current command

/

slash, slash (Filename path separator [forward slash]). .

1. As the separator of the path, there is only one slash in the path to represent the root directory, and the path starting with the slash represents the path starting from the root directory;

2. As operator, represents the division symbol. For example: a=4/2

`

Backquotes, backquotes (Command substitution[backquotes]). . The enclosed command can be executed and the execution result is assigned to a variable. For example: a=`dirname '/tmp/x.log'`. The result returned by dirname will be assigned to a. Note , here Mitchell specifically uses backticks and single quotes, pay attention to the difference

:

null command [colon]), what is this command? Neither is done, but there is a return value, and the return value is 0 (ie: true). The function of this command is very wonderful.

1. It can be used as a condition for while loop;

##2. Use it as a placeholder in the if branch (that is, when a certain branch does nothing);

3. Place it as a separator where there must be a binary operation, such as: ${username= `whoami`}

4. Assign a value to a string variable in parameter substitution, and truncate the length of a file to 0 in the redirection operation (>) (:>> When used like this, If the target exists, do nothing), this can only be used in ordinary files, not in pipes, symbolic links and other special files;

5. You can even use it to comment (the content after # will not be checked, but the content after : will be checked. If there is a syntax error in the statement, an error will be reported);

6. You can also use it as a domain separator, such as in the environment variable $PATH, Or in passwd, there is a colon as a domain separator;

7. You can also use a colon as a function name, but this will change the original meaning of the colon (if you accidentally use it as a function name, You can use unset -f: to undefine the function).

!

Exclamation mark (reverse (or negate) [bang], [exclamation mark]).

Negate a test result or exit status.

1. Indicates reverse logic, such as the following !=, which means not equal;

2. Indicates negation, such as: ls a[!0-9] #Indicates what follows a It is not a file followed by a number;

3. In different environments, the exclamation mark can also appear in indirect variable references;

4. In the command line, it can be used for historical commands To call the mechanism, you can try !$,!#, or !-3 to see, but be aware that this feature cannot be used in script files (it is disabled).

*

Asterisk (wildcard/arithmetic operator[asterisk]).

1. As a wildcard that matches file name extensions, it can automatically match every file in a given directory;

2. It can be used as a character qualifier in regular expressions to indicate the preceding The matching rules match any number of times;

3. Represents multiplication in arithmetic operations.

**

Double asterisk. In arithmetic operations, it means exponentiation operation.

?

Question mark (test operator/wildcard[Question mark]).

1. Represents conditional testing;

2. Represents C-style ternary operator ((condition?true-result:false-result)) within double brackets;

3. Used in parameter substitution expressions to test whether a variable has a value set;

4. As a wildcard character, used to match file name expansion properties and used to match a single character;

5. In a regular expression, it means matching the previous rule 0 or 1 times.

$

Dollar sign (Variable substitution[Dollar sign]).

1. As the leader of a variable, it is used as a variable replacement, that is, to quote the content of a variable, such as: echo $PATH;

2. In a regular expression, it is defined as the end of the line ( End offline).

${}

Parameter substitution (Variable substitution).

is used to represent variables in strings.

$'...'

Expand the quoted content and execute the escaped content within the single quotation marks (the single quotation marks are originally quoted as they are). In this way, one or Multiple [\] escaped octal, hexadecimal values ​​expanded to ASCII or Unicode characters.

$*

$@

Positional Parameters.

This is used when passing parameters when using script files. Both can return all parameters of the calling script file, but $* returns all parameters as a whole (string), while $@ returns each parameter as a unit as a parameter list. Note that you need to enclose $*, $@ in double quotes when using them. These two variables are affected by $IFS. If used in actual applications, some details should be considered.

$

# represents the number of parameters passed to the script.

$?

When this variable value is used, it returns the exit status code value of the last command, function, or script. If there is no error, it is 0, if it is non-0 , it means that there was an error in the last execution before this.

$$

Process ID variable, this variable saves the process ID value of running the current script.

()

Parenheses (parentheses).

1, Command group. A command enclosed by a set of parentheses is a command group, and the commands in the command group are executed in a subshell. Because it is running in a subshell, there is no way to get the value of the variable inside the brackets outside the brackets, but conversely, within the command group, you can get the outside value. This is a bit like the relationship between local variables and global variables. In implementation, if you encounter the need to cd to a subdirectory and return to the current directory after the operation is completed, you can consider using subshell to handle it;

2. Used for initialization of arrays.

{x,y,z,...}

Brace Expansion.

This expansion can be used to expand the parameter list in the command. The command will match and expand according to the pattern separated by brackets in the list. One thing to note is that there cannot be spaces in this curly brace expansion. If spaces are indeed necessary, they must be escaped or quoted using quotes. Example: echo {a,b,c}-{\ d," e",' f'}

{a..z}

This flower was added in Bash version 3 As an extension of bracket expansion, you can use {A..Z} to represent a list of all characters from A-Z. Mitchell tested this method and it seems that it only applies to A-Z, a-z, and numbers {minimum..max}. way to expand.

{}

Code blocks (curly brackets).

This is an anonymous function, but unlike a function, the variables in the code block can still be accessed after the code block. Note: There needs to be spaces and statements separated inside the curly braces. In addition, in xargs -i, it can also be used as a text placeholder to mark the location of the output text.

{} \;

This {} represents the path name. This is not built-in in the shell. From what I have seen now, it seems that it is only used in the find command. Pay attention to the semicolon at the end. This is the command sequence that ends the -exec option in the find command. When actually using it, it must be escaped to avoid being misunderstood by the shell.

[]

Square brackets.

1. Test representation, Shell will test the expression within []. It should be noted that [] is part of the Shell’s built-in test, rather than using the external command /usr/bin/test Link;

2. In the context of an array, it represents an array element. Fill in the position of the array element in the square brackets to obtain the content of the corresponding position, such as:

1. Array[ 1]=xxx

2. echo${Array[1]};

3. Indicates the range of the character set. In regular expressions, square brackets indicate the characters that can be matched at this position. Set range.

[[]]

Double brackets.

This structure is also a test, testing the expression in [[]] (Shell keyword). This is better than single square brackets in preventing logical errors in scripts. For example, the &&,||,<,> operators can pass the test in a [[]], but not in []. There is no filename expansion or word splitting in [[]], but parameter expansion and command substitution can be used. Filename wildcards and delimiters like whitespace are not used. Note that if octal, hexadecimal, etc. appear here, the shell will automatically perform conversion comparison.

$[...]

The word expression represents integer expansion.

Execute integer expressions inside square brackets. Example:

1. a=3

2. b=7

3. echo$[$a+$b]

4. echo$ [$a*$b]

5. ##The return is 10 and 21

(())

double parentheses.

represents integer expansion.

In the double bracket structure, all expressions can be like C language, such as: a++, b--, etc.

In the double bracket structure, all variables do not need to be prefixed with the "$" symbol.

Double brackets can perform logical operations and four arithmetic operations

The double bracket structure extends for, while, if conditional test operations

Supports multiple expression operations, each expression Use "," to separate

>

&<

>&

>>

<

<>

Redirection.

scriptname >filename redirects the output of scriptname to the file filename, and overwrites the original file if the filename file exists;

command &>filename redirects the standard output (stdout) of command and Standard error (stderr) to the file filename;

command >&2 Redirect the standard output (stdout) of command to the standard error (stderr);

scriptname >>filename Append the output of scriptname (same as >) to the file filename, or create the file if it does not exist. One > indicates that the file will be overwritten if it exists, and created if it does not exist. Two >> indicates that it will be created if it does not exist. If it exists, it will be added to the original one.

[i]<>filename Open filename The file is used for reading or writing, and i is assigned to the file as its file descriptor (file descriptor). If the file does not exist, it will be created.

<<

Double less then marks (here-document[double less then marks]).

This is also called Here-document and is used to redirect subsequent content to the stdin of the command on the left. <

<<<

Three less than signs (here-strings). Here-string is similar to Here-document, here-strings syntax: command [args] <<<["]$word["]; $word will be expanded and used as the stdin of command.

\<...\>

Word boundary.

This is a special delimiter used in regular expressions to mark word boundaries. For example: the will match there, another, them, etc. If you only want to match the, you can use this word boundary character, \ will only match the.

|

Pipe. Pipes are a concept that exists in both Linux and Unix. It is a very basic and important concept. Its function is to use the output (stdout) produced by the command before the pipe (on the left) as the input (stdin) of the command after the pipe (on the right). For example: ls | wc l, you can use pipes to connect commands together. Note: The standard output of each process in a pipe will be used as the standard input of the next command. The standard output during the period cannot cross the pipe and be used as the standard input of the subsequent command, such as: cat filename | ls -al | sort. Think about the output of this? Also, the pipe runs as a child process, so the pipe cannot cause variables to change.

>|

Force redirection.

This will force overwriting of already existing files.

&

Run job in background[ampersand]).

If the command is followed by an & symbol, the command will run in the background.

&&

||

Logical operator.

In the test structure, these two operators can be used to connect two logical values. || returns 0 (true) when one of the test conditions is true, and false if all are false; && returns true (0) when both test conditions are true, and false if any.

-

Minus sign, hyphen (Hyphen/minus/dash).

1. As an option, the prefix [option, prefix] is used.

2. Source or destination for stdin or stdout redirection [dash]. When tar does not have bunzip2 program patch, we can do this: bunzip2 linux-2.6.13.tar.bz2 | tar xvf - . Use the previously decompressed data as the standard input of tar (a - is used here)

Note: During implementation, if the file name starts with [-], then add this as a directional operation When echoing a variable, an error may occur. At this time, a suitable prefix path should be added to the file to avoid this situation. Similarly, when echoing a variable, if the variable starts with [-], it may also occur. Unexpected results. To be on the safe side, you can use double quotes to quote scalars:

var="-n"

echo$var

var="-n"

echo$var

Try to see what the output is?

Also, this representation method is not built-in in Bash. To achieve this effect, you need to see whether the software you are using supports this operation;

3. Represents the previous Therefore, if you cd to another directory and want to put back the previous path, you can use cd - to achieve the purpose. In fact, the [-] here uses the environment variable $OLDPWD. Note: [-] here is different from the previous point;

4. The minus sign or negative sign is used in arithmetic operations.

=

Equals.

1. When assigning a value to a variable, is there any space on both sides of the equal sign?

2. Appears as a comparison operator in a comparison test. Note here that if it is in square brackets To appear as a comparison, there need to be spaces on both sides of the equal sign.

+

Plus.

1. Arithmetic operator, indicating addition;

2. In a regular expression, it means that the preceding matching rule matches at least once;

3. As an option marker in a command or filter, use + in some commands or built-in commands to enable certain options, and use - to disable;

4. In parameter substitution (parametersubstitution), the + prefix means Substitute value (when the variable is empty, use the value after +)

%

percent sign (modulo[percent sign]).

1. In arithmetic operations, this is the modulus operator, which is the remainder after division of two numbers;

2. In parameter substitution (parametersubstitution), it can be used as a pattern match. Example:

                                                                                                                                                                                                                                                                                      to #       ##Start searching from the right (think about which symbol is from the left?)

      ##Any content between b and 9 (inclusive)

        ##The first one is to find The shortest matching item

        ##The last one is to find the largest matching item (greedy matching?)

~

tilde (Home directory[tilde]).

This is the same as the internal variable $HOME. By default, it indicates the current user's home directory (home directory). This has the same effect as ~/. If the tilde is followed by the user name, it indicates the user's home directory.

^

caret.

1. In regular expressions, as the beginning-of-line position identifier of a line;

2. In parameter substitution (Parametersubstitution), this usage has two ways One caret (${var^}), or two (${var^^}), respectively means the first letter is capitalized, and all capitals means (Bash version >=4).

Blank

Whitespace.

White space not only refers to spaces (spaces), but also includes tabs (tabs), blank lines (blank lines), or a combination of these. It can be used as a function delimiter to separate commands or variables. A blank line will not affect the behavior of the script, so it can be used to plan the script code to increase readability. The built-in special variable $IFS can be used to target certain commands. The input parameters are split, and the default is the whitespace character. If there are whitespace characters in a string or variable, you can use quotes to avoid possible errors.

The above is the detailed content of Summary of commonly used special characters in bash. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn