output redirection
>Represents outputting the correct output of the command to the specified file or device in an overriding manner.
>> stands for appending output.
Common output redirection:
- 1. Command >> File 2>&1 : Save both correct output and error output in the file by appending.
- 2. Command &> Filename: Save both correct and incorrect output in a file by overwriting.
- 3. Command & >> filename: Save both correct and incorrect output in a file in an appending manner.
-
4. Command >> File A 2 >> File B: The correct output is to the A file, and the wrong output to the B file.
Note: As far as error output is concerned, there must be no spaces on either side of > or >>.
example:
About the black hole: you can throw some commands that do not need to be displayed into the black hole (/dev/null)
input redirection
-
Command<filename: use the file as the input of the command, for example, the wc command counts lines, words and characters.
example:
Execute multiple commands sequentially
-
1. Command 1; Command 2: execute 1 and then execute 2, there is no logical connection between the commands
-
2. Command 1 && Command 2: && is a "selfish" symbol, that is, if command 1 is correct, I will execute command 2, and if you are wrong, I will not execute command 2.
- 3. Command 1 || Command 2: It is a spare tire symbol, that is, if 1 fails, 2 will go up, and if there is 1 line, I will not execute it.
pipe character
Command format: command A|command B, that is, the correct output of command 1 is the operation object of command B.
Other special symbols in Bash
- Single quotation marks '': All characters inside quotation marks are just characters and have no special meaning.
- Double quotation marks "": Except for $` and \, there is no special meaning.
- ": Backticks, the content inside the backticks is a system command, which will be executed first in Bash, such as aa=$(ls), then the result of ls execution will be assigned to aa.
- $(): Same as backticks, recommended.
- #: Represents comments in shell scripts.
- $: The value used to call the variable, equivalent to * in C language.
-
:Escapes.
Example: