Linux I/O redirection
Introduction
One of the powers of the Unix command line is the use of input/output redirection and pipes. In this session, we have covered the redirection of input, output and error streams.
stdin, stdout, and stderr
The bash shell has three basic streams; it takes input from stdin (stream 0), it sends output to stdout (stream 1) and it sends error messages to stderr (stream 2) .
The keyboard often serves as stdin, whereas stdout and stderr both go to the display. This can be confusing to new Linux users because there is no obvious way to recognize stdout from stderr. Experienced users know that separating output from errors can be very useful.
The next sections will explain how to redirect these streams.
output redirection
stdout ( > )
stdout can be redirected with a greater than sign. While scanning the line, the shell will see the > sign and will clear the file.
The > notation is, in fact, the abbreviation of 1> (stdout being referred to as stream 1).
Note that the bash shell effectively removes the redirection from the command line before argument 0 is executed. This means that in the case of this command:
the shell only counts two arguments (echo = argument 0, hello = argument 1). The redirection is removed before the argument counting takes place.
output file is erased
While scanning the line, the shell will see the > sign and will clear the file! Since this happens before resolving argument 0, this means that even when the command fails, the file will have been cleared!
noclobber
Erasing a file while using > can be prevented by setting the noclobber option.
overruling noclobber
The noclobber can be overruled with >|.
append (>>)
Use >> to append output to a file.
error redirection
2> stderr
Redirecting stderr is done with 2>. This can be very useful to prevent error messages from cluttering your screen.
The screenshot below shows redirection of stdout to a file, and stderr to /dev/null. Writing 1> is the same as >.
2>&1
To redirect both stdout and stderr to the same file, use 2>&1.
Note that the order of redirections is significant. For example, the command
directs both standard output (file descriptor 1) and standard error (file descriptor 2) to the file dirlist, while the command
directs only the standard output to file dirlist, because the standard error made a copy of the standard output before the standard output was redirected to dirlist.
output redirection and pipes
By default, you cannot grep inside stderr when using pipes on the command line, because the only stdout is passed.
With 2>&1 you can force stderr to go to stdout. This enables the next command in the pipe to act on both streams.
You cannot use both 1>&2 and 2>&1 to switch stdout and stderr.
You need a third stream to switch stdout and stderr after a pipe symbol.
joining stdout and stderr
The &> construction will put both stdout and stderr in one stream (to a file).
input redirection
stdin(<)
Redirecting stdin is done with < (short for 0<).
<< here document
The here document (sometimes called here-is-document) is a way to append input until a certain sequence (usually EOF) is encountered. The EOF marker can be typed literally or can be called with Ctrl-D.
check the below code
<<< here string
The here string can be used to directly pass strings to a command. The result is the same as using echo string | command (but you have one less process running).
See rfc 3548 for more information about base64.
confusing redirection
The shell will scan the whole line before applying redirection. The following command line is very readable and is correct.
But this one is also correct, but less readable.
Even this will be understood perfectly by the shell.
quick file clear
So what is the quickest way to clear a file ?
And what is the quickest way to clear a file when the noclobber option is set ?
Exercise, Practice and Solution:
1. Activate the noclobber shell option.
Code:
2. Verify that noclobber is active by repeating a ls on /etc/ with redirected output to a file.
Code:
3. When listing all shell options, which character represents the noclobber option?
Code:
4. Deactivate the noclobber option.
Code:
5. Make sure you have two shells open on the same computer. Create an empty tailing.txt file. Then type tail -f tailing.txt. Use the second shell to append a line of text to that file. Verify that the first shell displays this line.
Code:
6. Create a file that contains the names of five people. Use cat and output redirection to create the file and use a here document to end the input.
Code:
Previous:
Linux file globbing
Next:
Linux - Filters