I/O Redirection

Standard I/O Streams

  • Three Standard I/O file descriptors

  • By default bound to the terminal ⟶ keyboard and screen

Meaning

Name

Descriptor

C Macro

Standard input

stdin

0

STDIN_FILENO

Standard output

stdout

1

STDOUT_FILENO

Standard error

stderr

2

STDERR_FILENO

Important

A tradition which is religiously adhered to in Unix

  • Debug and error output goes to standard error ⟶ can be redirected to logfile, for example (or discarded into /dev/null)

  • Data goes to standard output ⟶ can be fed into another programm via the pipe

I/O Redirection Operators

command < file

command is fed file contents as standard input (i.e. as if typed on keyboard)

command > file

command writes standard output into file (not on screen)

command 2> file

command writes standard error into file (not on screen)

Variation: non-destructive redirection

  • > truncates file prior to writing, if it exists

  • >> appends to file (or creates if it does not exist)

Example: Standard Output Redirection

Say we are using the grep program to search /etc/passwd for lines containing the word jfasch. It writes the lines (only one in out case) that it finds to standard output:

$ grep jfasch /etc/passwd
jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash
../../../../../../_images/grep-no-redir.svg

Redirect its output into a file that we might keep for later use,

$ grep jfasch /etc/passwd > found.csv
$ ls -l found.csv
-rw-rw-r--. 1 jfasch jfasch 62 May 12 13:28 found.csv
$ cat found.csv
jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash
../../../../../../_images/grep-stdout-redir.svg

Example: Standard Input Redirection

Although grep can open its input file by itself when given as a second commandline parameter, it reads from standard input when not.

$ grep jfasch < /etc/passwd
jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash
../../../../../../_images/grep-stdin-redir.svg

Example: Standard Error Redirection

grep emits error messages on standard error in some cases, on of which is where it cannot find the input file given,

$ grep jfasch /etc/passwd-does-not-exist
grep: /etc/passwd-does-not-exist: No such file or directory

If, for example, we want to keep error messages for later debugging (a larger shell script, say), we can redirect standard error to a file. Remember, standard error is on file descriptor 2.

$ grep jfasch /etc/passwd-does-not-exist 2>errors.lst
$ ls -l errors.lst
-rw-rw-r--. 1 jfasch jfasch 60 May 12 14:21 errors.lst
$ cat errors.lst
grep: /etc/passwd-does-not-exist: No such file or directory
../../../../../../_images/grep-stderr-redir.svg

Another way of treating error is to ignore them. In this case, the character special device /dev/null is usually used (it behaves like a filefor the purpose of redirection):

$ grep jfasch /etc/passwd-does-not-exist 2>/dev/null

Example: Everything Redirection

If we want (not to say that we have to though!), we can combine redirections of all three standard streams in a way that grep

  • Reads from standard input

  • Writes standard output to a file

  • Write standard error to /dev/null

$ grep jfasch < /etc/passwd > found.csv 2>/dev/null
../../../../../../_images/grep-all-redir.svg

Important

When multiple redirections are used, the operations are executed from left to right!

This might not sound so important from what we’ve learned so far - but see, for example, I/O Redirection: Swap stdout And stderr.