Shell basics: Pipes and redirection

[linkstandalone]

Every (UNIX) process has (at least) 3 communication channels available to it: Standard input (STDIN), Standard output (STDOUT) and standard error (STDERR). These are initially inherited from the parent process, so the child process does not necessarily know where they lead. These can connect to a terminal, a file, a network connection or a channel belonging to another process.

Symlinks

Stdin, stdout, stderr are presented as files and are actually symlinks to /proc/self/fd/{0,1,2}, directory listing from /dev:


$ ls -la /dev/{stdin,stdout,stderr}
lrwxrwxrwx 1 root root 15 Jul 27 13:36 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Jul 27 13:36 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Jul 27 13:36 /dev/stdout -> /proc/self/fd/1

STDIN, STDOUT and STDERR are in fact guaranteed to correspond to file descriptors 0, 1 and 2. Normally in interactive terminal sessions, STDIN reads from the keyboard and STDOUT and STDERR write their output to the terminal window.

Symbols

An example of use of redirection to avoid spammy "Permission denied" output:

find / -type f -name test 2>/dev/null