Input/Output redirection
The Linux system usually reads the input from a place called the standard input and writes the result of a command to the standard output feedback to us; By default, this is also the terminal (command line) that we use. If we want to change the way we input and output, we need to use redirection. There are usually several ways to redirect commands:
Command |
Description |
Command > File |
Redirects the output to file. |
Command < file |
Redirects the input to file. |
Command >> file |
Redirects the output to file in an append manner. |
n > File |
redirect files with file descriptor N to file. |
n >> File |
Files with file descriptor n are redirected to file in an append manner. |
N >& m |
Merges the output file m and N. |
N <& m |
Merges the input file m and N. |
<< tag |
Enter the contents of the tag between tag and end tag tags as input. |
It is important to note that:
File descriptor 0 is typically standard input (STDIN)
1 is standard output (STDOUT)
2 is standard error output (STDERR)
Example 1: Output redirection
!/bin/bashecho"stdout redirection""stdout redirection "redirected Files" myfile "echo"stdout redirection again" " >> " indicates that the redirected content is attached to a file and does not overwrite the previous content
Example 2: Input redirection
!/bin/bashecho < myfile #将输出 the contents of the "myfile" file
Here Document
Its function is to treat all content between two EOF as a whole text, syntax:
Command << EOF documenteof
Example:
#!/bin/bashcat << years old!! EOF
years old!!
Linux Learning notes-stdin/stdout redirection