YouTip LogoYouTip

Linux Shell Io Redirections

Most UNIX system commands accept input from your terminal and send the resulting output back to your terminal. A command usually reads input from a place called standard input, which, by default, is your terminal. Similarly, a command usually writes its output to standard output, which, by default, is also your terminal.\\n\\nThe list of redirection commands is as follows:\\n\\n| Command | Description |\\n| --- | --- |\\n| command > file | Redirect output to file. |\\n| command > file | Redirect output to file in append mode. |\\n| n > file | Redirect the file with file descriptor n to file. |\\n| n >> file | Redirect the file with file descriptor n to file in append mode. |\\n| n >& m | Merge output files m and n. |\\n| n <& m | Merge input files m and n. |\\n| < Note that file descriptor 0 is usually standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error (STDERR).\\n\\n* * *\\n\\n## Output Redirection\\n\\nRedirection is generally achieved by inserting specific symbols between commands. Specifically, the syntax for these symbols is as follows:\\n\\ncommand1 > file1\\nThe above command executes command1 and then stores the output content into file1.\\n\\nNote that any existing content in file1 will be replaced by the new content. If you want to append the new content to the end of the file, please use the >> operator.\\n\\n### Example\\n\\nExecute the following who command, which redirects the complete output of the command to the user file (users):\\n\\n$ who > users\\nAfter execution, there is no output on the terminal because the output has been redirected from the default standard output device (terminal) to the specified file.\\n\\nYou can use the cat command to view the file content:\\n\\n$ cat users _mbsetupuser console Oct 31 17:35 tianqixin console Oct 31 17:35 tianqixin ttys000 Dec 1 11:33 \\nOutput redirection will overwrite the file content, see the following example:\\n\\n$ echo ":example.com" > users $ cat users :example.com $\\nIf you do not want the file content to be overwritten, you can use >> to append to the end of the file, for example:\\n\\n$ echo ":example.com" >> users $ cat users :example.com :example.com $\\n\\n* * *\\n\\n## Input Redirection\\n\\nJust like output redirection, Unix commands can also take input from a file, the syntax is:\\n\\ncommand1 ), while input redirection uses the less-than sign (<).\\n\\n### Example\\n\\nContinuing with the above example, we need to count the number of lines in the users file, execute the following command:\\n\\n$ wc -l users 2 users\\nYou can also redirect input to the users file:\\n\\n$ wc -l < users 2 \\nNote: The results of the two examples above are different: the first example will output the file name; the second will not, because it only knows to read content from standard input.\\n\\ncommand1 outfile\\nSimultaneously replace input and output, execute command1, read content from the file infile, and then write the output to outfile.\\n\\n### In-depth Explanation of Redirection\\n\\nUnder normal circumstances, each Unix/Linux command opens three files when running:\\n\\n* Standard input file (stdin): The file descriptor of stdin is 0, Unix programs read data from stdin by default.\\n* Standard output file (stdout): The file descriptor of stdout is 1, Unix programs output data to stdout by default.\\n* Standard error file (stderr): The file descriptor of stderr is 2, Unix programs write error messages to the stderr stream.\\n\\nBy default, command > file redirects stdout to file, and command file\\nIf you want to append stderr to the end of the file, you can write:\\n\\n$ command 2>>file\\n**2** represents the standard error file (stderr).\\n\\nIf you want to merge stdout and stderr and then redirect to file, you can write:\\n\\n$ command > file 2>&1Or $ command >> file 2>&1\\nIf you want to redirect both stdin and stdout, you can write:\\n\\n$ command file2\\nThe command redirects stdin to file1, and stdout to file2.\\n\\n* * *\\n\\n## Here Document\\n\\nHere Document is a special redirection method in Shell, used to redirect input to an interactive Shell script or program.\\n\\nIts basic form is as follows:\\n\\ncommand < Note:\\n> \\n> \\n> * The ending delimiter must be written at the beginning of the line, with no characters before or after it, including spaces and tab indentation.\\n> * Spaces before and after the starting delimiter will be ignored.\\n\\n### Example\\n\\nCount the number of lines of the Here Document using the wc -l command on the command line:\\n\\n$ wc -l << EOF Welcome to example.com EOF 3 # Output is 3 lines $\\nWe can also use Here Document in a script, for example:\\n\\n#!/bin/bash# author:# url:example.com cat < /dev/null\\n/dev/null is a special file; any content written to it will be discarded; if you try to read content from this file, you will read nothing. However, the /dev/null file is very useful; redirecting the output of a command to it will have the effect of "suppressing output".\\n\\nIf you want to suppress both stdout and stderr, you can write:\\n\\n$ command > /dev/null 2>&1\\n> **Note:** 0 is standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error (STDERR).\\n> \\n> \\n> There cannot be a space between **2** and **>** here; **2>** only represents error output when written together as a single unit.
← Docker Compose Down CommandDocker Compose Restart Command β†’