IO redirection - Linux
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
Standard Input
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.
Standard Output
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.
Standard Error
The standard error send the errors to output generated by a program during execution.
ls %
The result will be
ls: cannot access %: No such file or directory
This is an example for standard error.
Stream Redirection
Linux have the option to redirect this streams.
The commands with a single bracket overwrite the destination’s existing contents.
Overwrite
> - 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.
Append
>> - standard output
<< - standard input
2>> - standard error
Examples
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.
Special cases
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.
Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.
Recent Posts
- Disabling Payment Methods in WooCommerce Based on Conditions
- How to Update Product Quantity in WooCommerce Using Custom Code
- Dynamically Generating a Table of Contents in WordPress
- Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
- Understanding the Impact of git reset --hard Command
Your Questions / Comments
If you found this article interesting, found errors, or just want to discuss about it, please get in touch.