Pipe character ("|")
Use the output of the preceding command as the input of the following command. Multiple commands can be connected using multiple pipe characters.
# Output the string 'hello world' to the wc command to count the number of words
echo "hello world" | wc -w
# output:
2
output redirection character (">")
Redirect the output of the command to a file. Redirection explanation: Generally, the program will output to the standard output stdout, which is usually the terminal being used. The redirection character redirects the standard output to a place other than the standard output.
# Redirect (write) the string hello world to the file hello.txt
# If the file does not exist, create the file; if the file exists, overwrite the file content
echo "hello world" > hello.txt
# Output the content of the hello.txt file
cat hello.txt
# output:
hello world
Append redirection character (">>")
Append the output of the command to a file.
# Append the string hello world to the file hello.txt
# If the file does not exist, create the file; if the file exists, append the content to the end of the file
echo "hello world" >> hello.txt
# Output the content of the hello.txt file
cat hello.txt
# output:
hello world
hello world
The output of the echo command is one line by default, so there is a newline at the end of the output.
input redirection character ("<")
Take a file as input to the command.
echo "hello world" > hello.txt
# Use the file hello.txt as input to the wc command
wc -w < hello.txt
# output:
2
Multi-line input redirector ("<<")
Also known as Here Document, it uses a multi-line string as the input to the command.
# Use a multi-line string as input to the wc command
# It is just a convention to use EOF (end of file) as the end marker, others can be used.
# EOF cannot have any characters or spaces before and after it.
wc -w << EOF
> hello world
> hello world
> EOF
# output:
4
Special redirection symbols
Running the program will open the following three files:
Standard input file (stdin)
The file descriptor of stdin is 0, and the program reads data > from stdin by default.
Standard output file (stdout)
The file descriptor of stdout is 1, and the program outputs data > to stdout by default.
Standard error file (stderr)
The file descriptor of stderr is 2, and the program writes error > information to the stderr stream.
Error output redirector ("2>")
# Redirect the error output of the command to the file error.txt
# If the file does not exist, create the file; if the file exists, overwrite the file content
ls not-exist-directory/ 2> error.txt
# Output the content of the error.txt file
cat error.txt
# output:
ls: cannot access 'not-exist-directory/': No such file or directory
Combine standard output and error output (">&")
1>&2 correct return value is passed to 2 output channels &2 means 2 output channels
2>&1 error return value is passed to 1 output channel, &1 means 1 output channel
Be careful not to have spaces between these characters.
Create file structure
mkdir exist-directory
touch exist-directory/exist-file.txt
# Merge the error output of the command to the standard output channel
# and write to the file out.txt
ls not-exist-directory/ exist-directory/ > out.txt 2>&1
# Output the content of the out.txt file
cat out.txt
# output:
ls: cannot access 'not-exist-directory/': No such file or directory
exist-directory/:
exist-file.txt
# understand first
ls not-exist-directory/ exist-directory/ > out.txt
# equals
ls not-exist-directory/ exist-directory/ 1> out.txt
# This is to redirect standard output to the file out.txt, while error still goes to the terminal
# The same
ls not-exist-directory/ exist-directory/ 2> out.txt
# This is to redirect error output to the file out.txt, while standard output still goes to the terminal
# Merge the standard output of the command into the error output
# and write the combined error output to the file out.txt
ls not-exist-directory/ exist-directory/ 2> out.txt 1>&2
# Output the content of the out.txt file
cat out.txt
# output:
ls: cannot access 'not-exist-directory/': No such file or directory
exist-directory/:
exist-file.txt
Combine output to file ("&>")
# Merge the standard output and error output of the command into the file out.txt.
# is a shorthand for the above two methods
ls not-exist-directory/ exist-directory/ &> out.txt
# Output the content of the out.txt file
cat out.txt
# output:
ls: cannot access 'not-exist-directory/': No such file or directory
exist-directory/:
exist-file.txt
Therefore, common commands such as:
xxx &> /dev/null
xxx > /dev/null 2>&1
It means redirecting both the standard output and error output of the xxx program to the /dev/null file, i.e. discarding the output.
Very helpful :D, arch tutorial next?