This lesson explores advanced mechanics of standard output and error in Bash for robust shell scripting.
In this lesson, we dive into advanced mechanics of standard output (stdout) and standard error (stderr) in Bash. Building on basic shell scripting, mastering these streams—especially when paired with redirection and pipelines—is essential for robust, complex scripts.
Standard Output (stdout): the default channel for normal output.
Standard Error (stderr): the default channel for error and diagnostic messages.
Both streams appear in your terminal by default, but they serve different purposes. Think of them as two separate pipes under your sink: clean water (stdout) versus wastewater (stderr). Keeping them distinct helps you handle success and failure conditions independently.
By default, > captures only stdout. To see how this works, consider redirecting the output of an echo:
Copy
Ask AI
# Overwrite or create file.txt with stdoutecho "hello" > file.txt# Append to file.txt instead of overwritingecho "hello again" >> file.txtcat file.txt# Output:# hello# hello again