Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Various redirection characters (numbers)

>The right arrow indicates output redirection;<The left arrow indicates input redirection.

  • >Redirects output to a location that replaces all the contents of the original file. Such asThe command > fileTo redirect standard output to a file.

> 是 1 >The shorthand.

  • >>Redirection appends to a location to add content to the end of the original file. Such asThe command > > fileAppends the standard output redirection to the file.

>> 是 1 > >The shorthand.

  • <Redirect something to a location as standard input. Such asThe command < file, using the file as standard input to the command.

< is short for 0<. << indicates append input.

  • <<delimiterIt’s usually acommand<<After that, write next to the character as the delimiter, enter to write the content, end with the same delimiter, the content in the middle ascommandThe input. A common delimiter uses EOF. Such asCommand << delimiter, read from standard input until it stops at a delimiter

<< indicates append input. << is short for 0<<.

  • Command < file1 >file2 Takes file1 as the standard input of the command and prints the standard output to file2.

  • Command > file <

  • 2> Output error redirection to a file.

  • 2>> Append error redirection output to the end of the file.

  • &> mixed output of incorrect and correct information. Redirects both standard output and error output to a location.

  • &>> or Command >> file 2>&1 appends both standard output and error output redirection to the file.

instructions

  1. The arrow points to where the data is going.
  2. Standard input (English: stdin) : code 0, use < or <<. Data flows from right to left.
  3. Standard normal output (English: STdOUT) : code 1, using > or >>. Data flows from left to right.
  4. Standard error output (English: stderr) : code 2, use 2> or 2>>. Data flows from left to right.

Some small examples of redirection

Read the file and redirect the file content input to the variable

For example, there is a file1.txt file:

# cat file1.txt
this is a new line has been appended to multiple files
Copy the code

Use input redirection to enter the contents of a file into a variable

# read content<file1.txt
Copy the code

Output view variable content

# echo $content
this is a new line has been appended to multiple files
Copy the code

Redirect output to a file

Output the result of the ls -l command to a file and view the file contents.

# ls -l > lstest.txt
# cat lstest.txttotal 40 -rw-r--r-- 1 root root 0 Sep 21 17:57 cat_test.txt -rw-r--r-- 1 root root 1065 Sep 21 17:32 colortest.sh -rw-r--r-- 1 root root 55 Sep 21 20:36 file1.txt -rw-r--r-- 1 root root 55 Sep 21 20:36 file2.txt -rw-r--r-- 1 root root  55 Sep 21 20:36 file3.txt -rw-r--r-- 1 root root 42 Sep 21 18:07 file.txt -rw-r--r-- 1 root root 0 Sep 22 13:49 lstest.txt -rw-r--r-- 1 root root 42 Sep 21 20:47 mulline.txt -rw-r--r-- 1 root root 0 Sep 21 17:56 redirect.txt -rw-r--r-- 1 root root 78 Sep 21 16:09 test1.sh -rw-r--r-- 1 root root 46 Sep 21 14:38 test1.txt -rw-r--r-- 1 root root 47 Sep 21 16:01 test.sh -rw-r--r-- 1 root root 40 Sep 21 14:13 test.txtCopy the code

Error redirecting output

Error execution, no redirection output by default:

# ls -l abc > lstest1.txt
ls: cannot access abc: No such file or directory
# cat lstest1.txt
#
Copy the code

Error output to file:

# ls -l abc 2> lstest1.txt
# cat lstest1.txt
ls: cannot access abc: No such file or directory
Copy the code

Both standard and error output redirect apends to files

# ls -l abc lstest1.txt
ls: cannot access abc: No such file or directory
-rw-r--r-- 1 root root 49 Sep 22 13:53 lstest1.txt
# cat lstest1.txt
ls: cannot access abc: No such file or directory
# ls -l abc lstest1.txt &>> lstest1.txt
# cat lstest1.txt
ls: cannot access abc: No such file or directory
ls: cannot access abc: No such file or directory
-rw-r--r-- 1 root root 98 Sep 22 13:55 lstest1.txt
Copy the code

command > file1 <<delimiterexample

# cat >>test.txt <<EOF
input new line
EOF
# cat test.txt
input new line
Copy the code