DEV Community

Mahinsha Nazeer
Mahinsha Nazeer

Posted on • Originally published at Medium on

Standard Input, Standard Output, and Standard Error

A running program, or process, reads input and writes output. When you run a command from the shell prompt, it normally reads its input from the keyboard and sends its output to the terminal window.

A process uses numbered channels called file descriptors to get input and send output. All processes start with at least three file descriptors. Standard input (channel 0) reads input from the keyboard. Standard output (channel 1) sends normal output to the terminal. Standard error (channel 2) sends error messages to the terminal.

If a program opens separate connections to other files, then it might use higher-numbered file descriptors.

Redirect Output to a File

As viewed in the following table, redirecting only stdout does not suppress displaying stderr error messages on the terminal.

The Input/Output (I/O) redirection changes how the process gets its input or output. Instead of getting input from the keyboard, or sending output and errors to the terminal, the process can read from or write to files. With redirection, you can save the messages to a file instead of displaying the output on the terminal. Alternatively, you can use redirection to discard output or errors, so they are not displayed on the terminal or saved.

You can redirect a process stdout to suppress the process output from appearing on the terminal. If you redirect stdout to a file and the file does not exist, then the file is created. If the file does exist and the redirection does not append to the file, then the redirection overwrites the file's contents. To discard the output of a process, you can redirect to the empty /dev/null special file that discards channel output that is redirected to it.

Pipelines and I/O redirection both manipulate standard output and standard input. Pipelines send the standard output from one process to the standard input of another process. Redirection sends standard output to files, or gets standard input from files.

While piping stdout of one process will be the input of the second:

eg: process alpha | process beta

Few bash examples:

find /etc -name passwd 2> /tmp/errors : Redirect errors from the find command to the /tmp/errors file when viewing normal command output on the terminal.
find /etc -name passwd > /tmp/output 2> /tmp/errors : Save process output to the /tmp/output file and error messages to the /tmp/errors file.
find /etc -name passwd > /tmp/output 2> /dev/null : process output to the /tmp/output file and discard error messages.
find /etc -name passwd &> /tmp/all-message-output : Store output and generated errors together to the /tmp/all-message-output file.
find /etc -name passwd >> /tmp/all-message-output 2>&1 : Append output and generated errors to the /tmp/all-message-output file.

Reference: https://rol.redhat.com/rol/app/courses/rh124-9.0/
Enter fullscreen mode Exit fullscreen mode

Top comments (0)