Linux - Standard I/O Streams



Standard IO Streams refers to the following three streams which are provided by default by any Linux or Unix system.

  • Stdin Stream
  • Stdout Stream
  • Stderr Stream
Linux - Standard I/O Streams

As explained earlier, In Linux, everything is a file. Like directories, regular files, and even the devices are considered to be special files. Each file has an associated number which is called a File Descriptor or in short an FD and this FD is used to make any input or output operation on the associated file.

Stdin , Stdout and Stderr are terminal screen and their file descriptors are 0, 1, and 2 respectively.

Stdin Input Stream

This is referred to as the standard input and the associated file descriptor is 0. This is also represented as STDIN. The Linux program will read the default input from STDIN. Actually STDIN refers to the computer screen.

Most of Linux commands support standard Stdin stream which means instead of using files or command line input, we can feed data to the program directly using STDIN.

For example cat command will read input from the screen. Type the following command and start writing something as follows:

$ cat
This is text which I'm feeding using STDIN directly.
This is text which I'm feeding using STDIN directly.

$

Here cat command takes standard input and when you go to next line by using Enter key or exit using CTRL+D, it prints the same input to the display which is your standard output.

When you write a C, C++ or Java program and halts it to read some input from the screen, then Stdin stream is used to read the data from the input screen. You can send data to Stdin using the input redirection operator < as follows

$ cat < inputfile.txt
This is text which I'm feeding using STDIN directly.


$

Above command reads the input from inputfile.txt and redirect it to Stdin which is finally fed to the cat program to be displayed on the screen.

Stdout Output Stream

This is referred to as the standard output and the associated file descriptor is 1. This is also represented as STDOUT. The Linux program will write the default output at STDOUT.

Whenever a Linux program is executed, it’s output is sent to your computer screen’s File Descriptor which is then you see on the display screen. Alternatively you can send your program output to any file to be saved or to any printer to be printed.

Following command sends the output to Stdout so that it can be displayed on your screen:

$ ls -l
total 8
-rw-r--r-- 2 root root 132 May  1 07:18 filename
-rw-r--r-- 2 root root 132 May  1 07:18 hardlink
lrwxrwxrwx 1 root root   8 May  1 07:17 symlink -> filename

$

You can redirect your output to be saved in a file output.txt using the output redirection operator > as follows:

$ ls -l > output.txt


$

Or you can use the following command:

$ ls -l 1 > output.txt


$

Now none of the output will be displayed on the screen. Here we mentioned 1 > output.txt which means Stdout should be redirected to output.txt file. By default system considers it as Stdout. Next section will differentiate Stdout from Stderr.

Stderr Error Stream

This is referred to as the standard error and the associated file descriptor is 2. This is also represented as STDERR. The Linux program will write all the error messages at STDERR.

By default STDERR is your display screen but just like standard out, it can be redirected to a file to save the error messages.

Let’s try the same cat command with a file name which does not exit:

$ cat  nonexist.txt
cat: nonexist.txt: No such file or directory

$

Here nonexist.txt file does not exist, so an error is displayed on the screen as stderr by default.

You can save this error message using the file descriptor 2 as follows:

$ cat  nonexist.txt 2>err

$

This time your error message is saved in err file.

Advertisements