Standard Input (STDIN), Output (STDOUT), and Error (STDERR) Explained (part 2)

Ok, after the long part 1, we continue our journey with Standard Input or STDIN. Following the concepts we discussed in part 1, we know that STDIN has a standard file descriptor of 0. In other words, every process created by the kernel in UNIX, has file descriptor 0 assigned to STDIN.

STDIN is the way a process or a command reads the provided input to perform some action, to then, produce STDOUT, STDERR, or sometimes nothing (In UNIX nothing often means success).

Let’s dig into a practical example by continuing to expand the use of the ls command, but this time, we are going to use the STDOUT of the ls command as STDIN to another wonder UNIX command called “grep“. Do not worry much about what grep does, I will write a post about “grep” and the amazing things you can do with it. For now, imagine you can use “grep” to capture and display the input which contain a particular string.

The command we are going to investigate is:

ls -l / | grep “tmp”

This command simple lists the expanded content of the root directory “/”, and forwards the output by using the pipe “|”, to the “grep” command. The “grep” command uses STDIN to read the output provided by the “ls” command, and checks every line if it contains the string “tmp“, if the line contains the string, is printed, otherwise, it is discarded.

When you execute the command, you should get something like:

drwxrwxrwt 1 root root 512 May 6 21:59 tmp
Now you would say, what’s the big deal? Well, to understand the concept better, let’s eliminate the pipe and the “grep” command, so let’s only type the following command:
ls -l /
Now we get a much larger output from the command:
total 620
lrwxrwxrwx 1 root root 7 Mar 24 17:40 bin -> usr/bin
drwxr-xr-x 1 root root 512 Mar 24 17:47 boot
drwxr-xr-x 1 root root 512 May 7 15:55 dev
drwxr-xr-x 1 root root 512 May 7 15:55 etc
drwxr-xr-x 1 root root 512 Apr 9 16:31 home
-rwxr-xr-x 1 root root 632096 Apr 9 10:52 init
lrwxrwxrwx 1 root root 7 Mar 24 17:40 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Mar 24 17:40 lib32 -> usr/lib32
lrwxrwxrwx 1 root root 9 Mar 24 17:40 lib64 -> usr/lib64
drwxr-xr-x 1 root root 512 Mar 24 17:40 media
drwxr-xr-x 1 root root 512 Apr 9 16:27 mnt
dr-xr-xr-x 9 root root 0 May 7 15:55 proc
drwx------ 1 root root 512 Apr 11 11:14 root
drwxr-xr-x 1 root root 512 May 7 15:55 run
lrwxrwxrwx 1 root root 8 Mar 24 17:40 sbin -> usr/sbin
drwxr-xr-x 1 root root 512 Mar 24 17:42 snap
drwxr-xr-x 1 root root 512 Mar 24 17:40 srv
dr-xr-xr-x 12 root root 0 May 7 15:55 sys
drwxrwxrwt 1 root root 512 May 6 21:59 tmp
drwxr-xr-x 1 root root 512 Mar 24 17:41 usr
drwxr-xr-x 1 root root 512 Mar 24 17:42 var

Do you see the difference? By adding the pipe “|” and the “grep” command which filters the presence on the string “tmp”, the rest of the output is discarded.

To conclude, grep used STDIN to read the larger output produce by the “ls -l /” command, read it, and excluded all the lines not containing “tmp”.

STDIN can be feed to a program also by applying the “<” redirection. Remember in part 1? We used “>” to redirect the output to a file? This does exactly the inverse, reads from the file, and redirects the content of the file to STDIN. For example:

grep “tmp” < /tmp/filetoparse.txt

Hope by now, STDIN, STDOUT, and STDERR are clearer. The best way to expand your knowledge on the subject is by practicing with the commands.

Thanks for reading and type away!!!

Leave a Reply