← Back Home

a quick introduction to standard streams, file descriptors, and redirection

  1. general
  2. blog

standard streams are pre connected input and output communication channels between a program and its environment.

The three standard streams are stdin, stdout, and stderr.

File descriptors are handles that are used to access an input or output file, stream, pipe, socket, device, network interface etc. File descriptors work by providing a layer of abstraction between an actual hardware device and a special file created by the kernel for the device, populated by udev, and stored in the /dev directory. When a process makes a call for read/write, file descriptors provide a way to correctly route the call.

Name Stream Default File Descriptors
Standard Input stdin Keyboard 0
Standard Output stdout Screen 1
Standard error stderr Screen 2

File Redirection is a common form of redirection. In file redirection, the FD for stdout is pointed to a file, rather than a screen. output redirection is accomplished by using greater-than sign >.

$ echo "Write to a file" > testfile

When above command is run, instead of displaying the text on screen, text is redirected to file. If the file wasn’t already there, it will be created and written to.If the file existed, it will be overwritten. To avoid overwriting an existing file, use >> output redirection operator, which append the data to the end of file.

Input redireciton is accomplished by using < operator.

$ cat < testfile

File Descriptor Redirection

We can manually specify which file descriptor to redirect. It is accomplished by adding a numerical prefix of the standard stream we wish to redirect.

Redirecting the stdout if a command to a file will look like this:

$ command 1> file

redirecting file to stdin will look like this:

$ command 0< file

Since the defaults (naked descriptors) do not specify stderr, we need to specify them manually.

$ rm testfile 2>/dev/null

This will discard any message generated by rm to /dev/null. But a better idea will be to redirect the message to a file.

$ rm testfile 2> errfile

Sometime we need to redirect more than one file descriptors. We can accomplish this by

$ cat * > cat.log 2>&1

This will redirect or stdout and sterr to cat.log.

It is important to know that file descriptors are read from left to right.

Here document << are perfect for embedding some data into a script.

$ wc -c <<EOF
> Hello
> EOF
5

Here string <<< is very similar to Here document but only works for a string.

$ wc -c <<< "I am string"
12