I/O Redirection – STDIN, STDOUT, STDERR streams

Unix and Linux treat data streams and peripherals device files as files. By default there are always three files open, stdin (keyboard), stdout (display) and stderr (error messages outputted on the screen). As with every open file, it is possible to redirected the three files. For instance you can redirect (capture) the output of a command, file, script and send it to another command, file or script as input.

In UNIX and Linux every open file is assigned a file descriptor. The operating system (UNIX and Linux ) needs to keep track of which files are open and it does this by assigning a number to an open file. (A file descriptor can be considered as a simplified type of file pointer or file handle in C programming language The file descriptors for stdin, stdout, and stderr are 0, 1, and 2. There are also file descriptors 3 to 9, that can be used to open additional files. These remaining file descriptors can be useful to assign to stdin, stdout, or stderr as a temporary duplicate link.

stdin, stdout and stderr stream (communication between display, keyboard and running program).

That’s the theory behind I/O redirection, so let’s take a look at some examples.

Command Syntax of Redirecting stdout Stream

Redirecting of the stdout stream is done using > or >> signs. The command syntax is as follows:


    output > file   

Redirect the output (stdout) to a file. The file is created if not present, otherwise it overwrites the file.


    output >> file   

Redirect the output (stdout) to a file. The file is created if not present, otherwise appends to the file.

Redirecting stdout Stream Examples

Let us take a look at some actual examples on the command line of a Linux system.


    > somefile    

The > truncates a file called somefilename to zero length. If the file is not present, it creates a zero-length file (same effect as ‘touch’ command (touch somefilename)).


    ls –l > listfile    

This command list the content of the current directory and writes the output to a file called listfile.

We could also use file descriptors numbers, so we get:


    filedescriptor > filename    

The default file descriptor is 1, but you can also set it. You can use them for instance in single line redirection commands. This are commands that only affect the line they are on. For instance to redirect stdout to a file after an echo in a script. Some single line redirections:


 # Redirect stdout to file "filename atorvastatin dose."
1>filename
# Redirect and append stdout to file "filename."
1>>filename
# Redirect stderr to file "filename."
2>filename
# Redirect and append stderr to file "filename."
2>>filename
# Redirect both stdout and stderr to file "filename."
&>filename

We can use the single line redirections in a script, for instance to echo messages to logfile. (Open een file using vi script.sh and copy the lines below to the file. Run the script by typing: /bin/bash script.sh).


LOGFILE=script.log

echo "This statement is sent to the log file" 1>$LOGFILE
echo "This statement is appended to the log file” 1>>$LOGFILE
echo "This statement is also appended to the log file" 1>>$LOGFILE
echo "This statement is echoed to stdout. It will not appear in the log”

Redirecting stderr Stream Examples

Let us take a look at some actual stderr examples on the command line of a Linux system.


2>&1

In the example above we show you a single line redirection that redirects stderr to stdout. Each error messages will be sent to same place as standard output (which default is the screen).
To redirect both stdout and stderr (overwriting the file if present).


cmd &> file.txt

Redirect both stdout and stderr appending to file can be done using the following syntax:


cmd >> file.txt 2>&1

Closing File Descriptors

It is also possible to close file descriptors for instance, closing the input file descriptor n.


n<&- 

Closing stdin.


0<&-, <&- 

Closing the output file descriptor n.


n>&- 

Closing stdout.


1>&-, >&- 

That’s all for this tutorial. We hope that you now know how to use STDIN, STDOUT, STDERR streams (I/O redirections). See you at the next Linux command-line tutorial.

This entry was posted in Linux Command Line. You can follow any responses to this entry through the RSS 2.0 feed. You can trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

Leave a Reply: