Standard Streams in Bash

Fumbani Banda Dec 31, 2021
  1. Standard Streams
  2. Standard Input
  3. Standard Input Using Keyboard
  4. Standard Input Using Files
  5. Standard Input From Redirection
  6. Standard Input From Piping
  7. Standard Output
  8. Standard Output to Display
  9. Standard Output Redirected to File
  10. Standard Output Through a Pipe
  11. Standard Error
  12. Standard Error Redirected to File
  13. Redirect All Output to File
Standard Streams in Bash

This tutorial explains the standard streams and redirecting output and input to and from a file.

Standard Streams

In Linux, there are three standard streams; standard input (stdin), standard output (stdout), and standard error (stderr). Each stream is represented by a file descriptor. The standard input is represented by a 0, the standard output by a 1, and the standard error by a 2.

Standard Input

The standard input is data that is being passed to a program. It is usually through typing on the keyboard. It can also be data that has been piped or redirected to the program. The standard input uses EOF (end-of-file) to indicate the end of input data.

Standard Input Using Keyboard

We will use the cat command to demonstrate the standard input using input data from the keyword. The cat command takes input and prints it out to the screen.

Open the Linux terminal and type cat, and press enter. You can now type any input, and once you press Enter, the cat command displays the input on the terminal. The cat command runs in a loop; to terminate it from receiving further input, press CTRL+D. It acts as a signal to indicate the EOF.

stdin-cat

Standard Input Using Files

Files can also provide input to a program. We will use the cat command to demonstrate that a file can be used as standard input.

The cat command takes the file name as an argument and displays the file’s content on the terminal. In the image below, the cat command takes the file, output.txt, and displays its content on the terminal.

stdin2-cat

Standard Input From Redirection

Standard input redirection uses the standard input redirection operator, <. Input redirection uses the notation command < filename. This means the input for the command comes from the contents inside the specified file name.

In the image below, the contents of the output.txt are passed to the cat command which prints them out on the terminal.

stdin3-cat

Standard Input From Piping

A pipe operator is used to pass the output of a command as input to another command. The pipe operator is a vertical bar, |.

Below, the head command gets the first 10 lines of the file, output.txt, and pipes it to the cat command as standard input. The cat command displays it as output to the terminal.

stdin4-cat

Standard Output

The standard output displays data that is generated by a program. This data is printed to the terminal if it is not redirected or piped.

Standard Output to Display

Almost any Linux command can be used to demonstrate the standard output. We will use the head command to demonstrate this. The head command takes a file name as an argument and prints out only the first 10 lines of the file.

stdout-head

Standard Output Redirected to File

Standard output redirection uses the standard output redirection operator, >. Output redirection uses the notation command > filename. It means the command’s output should be written to the filename specified.

The notation command >> filename means appending the command’s output to the file instead of overwriting the existing file. The >> is called the append redirection operator.

We use the output redirection operator to redirect the output of the ls command to the output.txt file. The cat command is used to display the contents of the file passed as an argument.

stdout-redirection

Standard Output Through a Pipe

A pipe operator is used to pass the output of a command to another command. The pipe operator is a vertical bar, |.

Here, the output of the ls command is piped to the head command. The head command only displays (standard output) the first 5 lines to the terminal from the input that was piped to it.

stdout-pipe

Standard Error

The standard error is similar to the standard output except that it is used for error messages from a program.

We will use the ls command to demonstrate the standard error. The ls command is used to display the contents inside a folder. We will pass a folder name that does not exist as an argument to the ls command, and it will display an error message using the standard error.

stderror-folder

Standard Error Redirected to File

We use the standard output redirection operator to redirect the standard error to a file. However, we have to specify that we would like to redirect the standard error by adding 2 to the output redirection operator, 2> as the file descriptor.

Let us now redirect the standard error of the ls command to a file. The 2> specifies that only errors generated by the ls command should be redirected to the errors_file.txt file.

stderr-file

Let us use the cat command to display the content of the errors_file.txt. Below, we see that the file contains the error message of the ls command.

stderr-cat

Redirect All Output to File

To redirect both the standard output and standard error to the same file, use the following notation,

command > file 2>&1. It redirects both streams of the command to the specified file.

In the image below, both the standard output and standard error of the ls command are redirected to the same file, output.txt.

output-both

Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub