Explanation of Standard input-output in Unix and Linux

Once a command is run, a process is created. This process then opens three flows:

stdin, called the standard input, where the process will read the input data. By default stdin refers to the keyboard; STDIN is identified by the number 0;

stdout, called standard output, where the process will write the output data. By default, stdin refers to the screen; STDOUT is identified by the number 1;

stderr, called standard error, where the process will write error messages. By default, stderr refers to the screen. STDERR is identified by the number 2;

By default, whenever a program is run data is read from the keyboard and the program sends its output and errors to the screen. However, it is also possible to read data from any input device, even a file, and send the output to a display device, a file, etc.

Here is an example for error redirection.

This is a very popular feature that many Unix users are happy to learn. In case you have worked with Unix for some time, you must have realised that for a lot of commands you type you get a lot of error messages. And you are not really bothered about those error messages. For example whenever I perform a search for a file, I always get a lot of permission denied error messages. There may be ways to fix those things. But the simplest way is to redirect the error messages elsewhere so that it doesn’t bother me. In my case I know that errors I get while searching for files would be of no use to me.

Here is a way to redirect the error messages

$ myprogram 2>errorsfile

This above command would execute a program named ‘ myprogram ‘ and whatever errors are generated while executing that program would all be added to a file named ‘ errorsfile ‘ rather than be displayed on the screen. Remember that 2 is the error output file descriptor. Thus ‘ 2> ‘ means redirect the error output.

$ myprogram 2>>all_errors_till_now

The above command would be useful in case you have been saving all the error messages for some later use. This time the error messages would append to the file rather than create a new file.

You might realize that in the above case since I wasn’t interested in the error messages generated by the program I redirected the output to a file. But since those error messages don’t interest me I would have to go and delete that file created every time I run that command. Else I would have several such files created all over whenever I redirect my unwanted error output. An excellent way around is shown below
$ find / -name s*.jpg 2>/dev/null

What’s /dev/null ????? That something like a black hole. Whatever is sent to the ‘ /dev/null ‘ never returns. Neither does one know where it goes. It simple disappears. Isn’t that fantastic !! So remember.. whenever you want to remove something.. something that you don’t want …you could just send it to /dev/null.

About Andrew Lin

Hi, I have always wanted to creat a blog site but never had the time. I have been working in Information Technology for over 15 years. I specialize mainly in networks and server technologies and dabble a little with the programming aspects. Andrew Lin

View all posts by Andrew Lin →