The IO redirection is an important feature in Linux. The redirection means capturing output from a file, command, program, script, or even code block within a script and sending it as input to another file, command, program, or script.
In default, there is three streams in Linux environment.
- Standard input (stdin) - eg: keyboard
- Standard output (stdout) - eg: screen
- Standard error (stderr) - eg: error messages output to the screen
This three streams are also numbered:
- stdin - 0
- stdout - 1
- stderr - 2
The standard input stream typically carries data from a user, file etc to a program.
echo -n "Enter your name and press [ENTER]: " read name
In the above program, we pass an argument that is generated from user keyboard to the program.
The standard output send data to output that is generated by a program.
echo This is my line
The above line of code display a sentence in our terminal display.
The standard error send the errors to output generated by a program during execution.
The result will be
ls: cannot access %: No such file or directory
This is an example for standard error.
Linux have the option to redirect this streams.
The commands with a single bracket overwrite the destination’s existing contents.
> - standard output < - standard input 2> - standard error
The commands with a double bracket do not overwrite the destination’s existing contents. It append the new content to existing content.
>> - standard output << - standard input 2>> - standard error
echo Hello Word > newfile.txt
This line of code create a
newfile.txt and add the content
Hello Word to it. If the file exist & there is content, the previous content will be removed and new content will be updated.
echo New line of text >> newfile.txt
If there is a file
newfile.txt exist, a content
New line of text will be appended at the end of the line in that file. If the file is not exist, this command will create a new file and add the content to it.
wc < file.txt
The above command read the word count in a file. This is good example for standard input.
ls %l 2> error.log
In the above line of code, an error will be produced and that error will be redirected to
error.log file. In this example, the content will be overwrite with the existing content.
ls %l 2>> error.log
In the above line of code, there will be a chance for error & that error will be append to existing content in error.log.
ls www &> status.log
In some situation, there will be chance for standard error and standard output. In the above line of code, if there is folder exist with the name
www, we will get a standard output and if there is no such directory, we will get a standard error. In the above line of code, we are redirecting both standard error and standard output to same file. But the content is overwrite.
ls www &>> status.log
In the above line of code, there is chance for standard error and standard output. But the content is append to existing content instead of replacing existing content.
In some special situation, we have to prevent the output.
ls www > /dev/null
The above line of command will prevent the standard output from showing up on console. That means, if there is the standard error, it will be displayed.
ls www &> /dev/null
The above line of code will prevent the standard output and standard error from showing up on console.