Note: Different versions of Linux will differ, but they are very similar
The Linux command obtains the input from the standard input device (stdin) By default, outputting the result to the standard output device (STDOUT) display. In general, the standard input device is the keyboard, the standard output device is the terminal, that is, the display.
Output redirection
The output of the command can not only be a monitor, but it can also be easily transferred to a file, which is called output redirection.
The syntax for command output redirection is:
- $ Command > file
In this way, the contents of the output to the display can be redirected to the file.
For example, the following command will not see any output on the monitor:
- $ who > users
Open the users file and you can see the following:
$ cat Usersoko tty01 Sep 07:30ai tty15 Sep 13:32ruth tty21 Sep 10:10pat tty24 Sep 13:07steve tty25 Sep 12 13:03$
Output redirection overwrites the file contents, see the following example:
$ echo Line 1 > users$ cat usersline 1$
If you do not want the file content to be overwritten, you can use >> to append to the end of the file, for example:
$ echo Line 2 >> users$ cat usersline 1line 2$
Input redirect
As with output redirection, Unix commands can also get input from a file with the following syntax:
- Command < file
In this way, commands that would otherwise need to get input from the keyboard are transferred to the file read content.
Note: Output redirection is greater than sign (>), and input redirection is less than sign (<).
For example, to calculate the number of rows in the users file, you can use the following command:
$ wc-l users2 users$
You can also redirect the input to the users file:
$ wc-l < users2$
The WC command can output the number of bytes in the specified file, the word book, the number of lines
Note: The results of the above two examples are different: The first example outputs the file name, and the second one does not because it only knows what to read from the standard input.
Redirect in-depth explanation
In general, each Linux command will open three files when it is run:
- Standard input file (stdin): stdin file descriptor for 0,unix program reads data from stdin by default.
- Standard output file (stdout): StdOut file descriptor for 1,unix program outputs data to stdout by default.
- Standard error file (stderr): The stderr file descriptor for the 2,unix program writes an error message to the stderr stream.
By default, command > file redirects stdout to File,command < file to redirect stdin to file.
If you want stderr to be redirected to file, you can write:
- $command 2 > file
If you want stderr to append to the end of file, write this:
- $command 2 >> file
2 indicates a standard error file (stderr).
If you want to redirect stdout and stderr to file after merging, you can write:
- $command > File 2>&1
Or
- $command >> file 2>&1
If you want to redirect both stdin and stdout, you can write this:
- $command < file1 >file2
The command commands redirect stdin to File1, redirecting stdout to File2.
Output standard or standard output or standard error files can be entered into file. You can write this:
- $command &>>File
List of all redirected commands available
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. |
Here Document
Here document does not currently have a unified translation, which is now translated into "embedded documents". Here Document is a special redirection method in the Shell, which has the following basic form:
- Command << delimiter
- Document
- Delimiter
Its role is to pass the contents (document) between the two delimiter as input to the command.
Attention:
- The end of the delimiter must be shelf write, the front can not have any characters, the back can not have any characters, including spaces and tab indentation.
- The space before and after the beginning of the delimiter is ignored.
In the following example, the number of document rows is computed by the WC-L command:
$WC-L << EOF A simple lookup program for Good (and bad) restaurants in Cape town.eof3$
You can also use here Document in a script, for example:
- #!/bin/bash
- Cat << EOF
- A simple lookup program
- For good (and bad) restaurants
- In Cape town.
- Eof
Operation Result:
This was a simple lookup programfor good (and bad) Restaurantsin Cape town.
The following script saves the document to the Test.txt file through the VI editor:
- #!/bin/sh
- FileName=test.txt
- VI $filename <<endofcommands
- I
- This file is created automatically from
- A shell script
- ^[
- Zz
- Endofcommands
To run the script:
$ sh test.shVim:Warning:Input is not from a terminal$
Open Test.txt, you can see the following content:
$ cat test.txtthis file was created automatically froma shell script$
/dev/null file
If you want to execute a command, but you do not want the output to appear on the screen, you can redirect the output to/dev/null:
- $ Command > /dev/null
/dev/null is a special file, and the content written to it is discarded, and if you try to read from the file, nothing is read. However, the/dev/null file is very useful and redirects the output of the command to it, which results in a "no output" effect.
If you want to block stdout and stderr, you can write this:
- $ Command > /dev/null 2>&1
Redirection in the shell (input and output)