The Standard Streams and Redirection
Shivm Soaini
Math Major Turned AI Enthusiast: Mastering Computer Vision | Building Robust Python Code | Delving Deep into the Math of AI | Sharing Knowledge Through Writing | Ready to Transform Ideas into Reality
The three standard streams are communication channels between a computer program and its environment.
They are:
Standard Output
Standard output is a place to which a program or command can send information.
The information could go to a screen to be displayed, to a file, or even to a printer or other devices.
Standard Error
Commands and programs also have a destination to send error messages: standard error.
By default, the shell directs standard error information to the screen for us to read, but we can change the destination if needed!
Standard Input
Standard input is where a program or command gets its input information from. By default, the shell directs standard input from the keyboard.
The input information could come from a keyboard, a file, or even from another command!
Redirection
"redirection" describes the ways we can alter the source of standard input, and the destinations for the standard output and standard error.
Redirecting Output
The redirect output symbol (>) tells the shell to redirect the output of a command to a specific file instead of the screen.
By default, the date command will print the current date to the screen.
If we instead run date > output.txt, the output will be redirected to a file called "output.txt".
command > filename
Note: If you redirect an output to an existing file, the contents of that file will be overwritten.
Appending
When we redirect output into a file using >, any existing contents in the file are overwritten. Sometimes this isn't what we want!
To instead keep the existing contents in the file and add new content to the end of the file, use >> when redirecting.
Redirecting Input
To pass the contents of a file to a standard input, use the < symbol followed by the filename.
For example, we could pass the contents of the chickens.txt file to the cat command using cat < chickens.txt
cat (and many other commands) are set up to accept filenames as arguments directly, but we can also redirect to standard input manually.
command < filename
Redirecting Standard Input and Output Together
We can redirect standard input and output at the same time.
In this example, we are using cat to read in the contents of "original.txt" file and then redirecting the output to a file called "output.txt".
cat < original.txt > output.txt
Let's take another example, we sort the contents of a file named "names.txt" and output them to a new file named "sortednames.txt".
sort < names.txt > sortednames.txt
Redirecting Standard Error
By default, error messages are output to the screen, but we can change this by redirecting standard error.
The standard error redirection operator is 2>
If we ran a command like cat nonexistentfile (where the file really does not exist), we would see an error printed to the screen. We can instead redirect standard error to a file with cat nonexistentfile 2> error.txt
cat nonexistentfile 2> error.txt
Note: If you want to redirect multiple standard errors to the same file use 2>> symbol to append instead of 2>.
Why 2> symbol?
Each stream gets its own numeric file descriptor, and for standard error the number is 2.
The > operator actually defaults to using 1 as the file descriptor number, which is why we didn't need to specify 1> to redirect standard output.
Similarly, the < operator uses a default file descriptor number of 0, so we don't need to specify 0< to redirect to standard input (though we can!).
Putting it all together
We can redirect multiple streams at once!
In this example, we are concatenating two files, redirecting the standard output to a file called pets.txt, and redirecting standard error to a file called error.txt
cat owls.txt cats.txt > pets.txt 2> error.txt
Redirecting Output and Error to the same file
If we wanted to redirect both standard output and standard error to the same file, we could either do this..
ls docs > output.txt 2> output.txt
Or we could instead use 2>&1 which is a fancy syntax for saying "redirect standard error to the same location as standard output" (or whatever has the file descriptor 1).
ls docs > output.txt 2>&1
Note: Newer versions of bash also support a fancier syntax for redirecting both standard output and standard error to the same file: the &> notation.
ls docs &> output.txt
ls docs &>> output.txt
Note : When redirecting both standard output and standard error, make sure standard output comes FIRST. Always redirect standard error after standard output.
Thanks for reading : )
If you find my content helpful, let me know with your comments. Any suggestions or insights are welcome !