Standard Input, Output and Error | Bash

In Bash, standard input (stdin), standard output (stdout), and standard error (stderr) are three streams that handle input and output for command-line processes. These streams allow communication between the user, the shell, and the executed commands.

  1. Standard Input (stdin): This is the default source of data for your script. Imagine it as a straw your script sips information from. By default, stdin points to your keyboard, ready to accept whatever you type.
  2. Standard Output (stdout): This is where your script outputs its results. Think of it as a loudspeaker broadcasting what your script has accomplished. By default, stdout goes straight to your terminal screen.
  3. Standard Error (stderr): This is a separate channel for error messages and warnings. Picture it as a red warning light flashing when something goes wrong. stderr also goes to your terminal by default, but it's distinct from stdout and often displayed in a different color.

These streams work together seamlessly, allowing your script to interact with data and display information. Let's see some examples:

Standard Input (stdin)

The stdin is the default input stream. It provides a way for commands to receive input from the user or from another command.

Example: Using the read command to take user input:
echo "Enter your name:" read name echo "Hello, $name!"

Standard Output (stdout)

The stdout is the default output stream. It is used to display the output of commands or scripts.

Example: Redirecting output to a file using >:
echo "This is some content." > output.txt

Standard Error (stderr)

The stderr is used to handle error messages and diagnostics. It allows separating error messages from regular output, which can be helpful for debugging.

Example: Simulating an error and redirecting stderr to a file using 2>:
command_that_does_not_exist 2> error.log

Combining stdout and stderr

It's possible to combine both stdout and stderr, redirecting them to the same location. This is useful when you want to capture both regular output and error messages.

Example: Redirecting both stdout and stderr to a file:
command_with_error > output_and_error.log 2>&1

In this example, 2>&1 means "redirect stderr to the same location as stdout."

Redirecting streams

The concept of redirecting streams in Bash involves changing the default destinations of the standard input (stdin), standard output (stdout), and standard error (stderr) using redirection operators. Here's an example using the ls command:

Redirecting Standard Output (stdout)

Example: ls -l > file_list.txt

In this example, the ls -l command lists the contents of a directory in long format, and > file_list.txt redirects the standard output to a file named file_list.txt. Instead of displaying the output on the screen, the result is saved into the specified file.

$ ls -l > file_list.txt

After running this command, the contents of the directory will be written to the file_list.txt file, and the terminal will not display the usual output.

Redirecting Standard Input (stdin)

Example: sort < unsorted.txt > sorted.txt

In this example, the sort command is used to sort the lines of text in a file. The < unsorted.txt part redirects the standard input to read from the file unsorted.txt. The sorted output is then redirected to the file sorted.txt using > sorted.txt.

$ sort < unsorted.txt > sorted.txt

After running this command, the sorted content will be saved in the sorted.txt file.

Conclusion

Standard input, output, and error streams are fundamental for understanding and managing the flow of data between commands in a Bash script or in the command line. Redirection operators (>, >>, <, | , 2>, etc.) are commonly used to manipulate these streams. They allow you to control where the input comes from, where the output goes, and where errors are logged.