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.
- 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.
- 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.
- 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: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 >: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>: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: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.txtIn 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.
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.txtIn 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.
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.
- Understanding Bash script structure and syntax
- Shebang and Script Execution permissions
- Create and Run Your First Bash Shell Script
- Writing Comments in Bash Scripts
- Variable Declaration and Assignment in Bash
- Bash Local and Global Variables
- Reading User Input in Bash
- String Manipulation in Bash
- Bash Arrays | An introduction to Bash arrays
- The Pipe '|' Operator in Bash (Advanced)
- Conditional Expressions in Bash
- Read and Write to Files with Bash
- Command Substitution in Bash Shell
- Error handling in Bash scripts
- Checking exit codes in bash
- Shell Expansion | Bash