Linux Topic
   >  Why use the Command Line?
   >  Common Linux Commands
   >  Directory Structure
   >  Using Regular Expressions
   >  $STDIN; $STDOUT and $STDERR
   >  IO Redirection and Pipes

 

$STDIN, $STDOUT and $STDERR

The Difference between $STDIN, $STDOUT and $STDERR

Pretty much every Linux command implemented uses three basic Input/Output streams -one for input and two for output:

I/O DescriptorDescription
$STDIN

This is the input stream that the command/script will use. It defines where the input should come from. For example, in the command "more oldfile1", $STDIN will be set to the file oldfile1.

Note: the default $STDIN for most functions is the keyboard

$STDOUT

This is the output stream that the command/script will use. It defines where the output should go. For example, in the command "cp oldfile1 > newfile", $STDOUT will be set to the file newfile.

Note: the default $STDOUT for most functions is the screen

$STDERR

This is the error message stream that the command/script will use. It defines where any warnings/errors should be sent. For example, in the command "cp oldfile1 newfile 2> myerrors", any errors will be set to the file myerrors.

Note: the default $STDERR for most functions is the screen

Having these three basic streams makes I/O Redirection possible for almost all Linux commands : i.e. the ability to change where the input comes from and the output and errors sent. When using Linux commands (-and especially when combining them), always be aware of the above three streams and what they should be set to. For example, consider this command to search files under the / for the string "abc":

$ grep "abc" /*
/* this is file "abc" */
grep: /lost+found: Permission denied
grep: /root: Permission denied

The $STDIN for this command has been set to all the files in the root directory (/*) but which stream(s) have the screen output come from? Well, it's actually come from two: the first line (valid output) has come from $STDOUT and the second and third lines (error messages) have come from $STDERR.


HomeSite IndexDesktop GuideServer GuideHints and TipsHardware CornerVideo SectionContact Us

 sitelock verified Firefox Download Button