17. Bash Redirections
Overview¶
Bash provides redirection mechanisms that allow you to control where command input, standard output, and standard error are sent.
Redirection is especially useful when writing scripts, saving command output to files, suppressing errors, logging program execution, and combining normal output with error messages.
Standard Streams¶
Linux commands normally work with three standard streams:
| File Descriptor | Name | Purpose |
|---|---|---|
0 |
STDIN | Standard input |
1 |
STDOUT | Standard output |
2 |
STDERR | Standard error |
By default:
The file descriptors can be explicitly referenced during redirection.
STDOUT to Screen¶
Standard output is displayed on the terminal by default.
Create a file:
Then run:
Output:
The output from ls is STDOUT, and because no redirection is specified, it is displayed on the screen.
This is equivalent to:
although explicitly redirecting STDOUT to itself is unnecessary.
STDOUT to a File¶
The > operator redirects STDOUT to a file.
Now the output is written to STDOUT.txt instead of being displayed on the terminal.
Read the file:
Output:
The general syntax is:
This is equivalent to:
Important: > Overwrites the File¶
If the file already exists, > replaces its contents.
The file contains only:
To append instead of overwrite, use >>:
Output:
STDERR to Screen¶
Standard error is also displayed on the terminal by default.
For example:
If file2 does not exist:
This message is sent through STDERR, not STDOUT.
STDERR to a File¶
Use 2> to redirect STDERR to a file.
If file1 exists and file2 does not, STDOUT remains on the screen:
The error is written to STDERR.txt.
Check the file:
Output:
The general syntax is:
This is useful when you want to separate normal output from error messages.
STDOUT to STDERR¶
STDOUT can be redirected to the same destination as STDERR using:
Example:
Normally, echo writes to STDOUT.
The following changes its destination so that STDOUT uses the same file descriptor as STDERR.
Run the script normally:
Output:
Now redirect STDOUT:
Check the file:
There is no output because the message was sent through STDERR.
Now redirect STDERR:
Check the file:
Output:
This demonstrates that the echo output was redirected from STDOUT to STDERR.
STDERR to STDOUT¶
The reverse operation is:
This redirects STDERR to the same destination currently used by STDOUT.
Example:
If access is denied, the error message is redirected to STDOUT.
Output:
Now redirect STDOUT to a file:
There is an important ordering detail here: redirections are processed from left to right.
A safer common pattern for sending both streams to one file is:
Here:
Therefore both streams go to the file.
Redirecting Both STDOUT and STDERR to the Same File¶
A common requirement is to save both normal output and errors in one log file.
Use:
This performs two operations:
Check the file:
Example:
Both STDOUT and STDERR are stored in the same file.
Using &> for Both Streams¶
Bash provides a shorter syntax:
For example:
Then:
Output:
The &> syntax redirects both:
This is convenient when writing Bash scripts.
Appending Both STDOUT and STDERR¶
To append both streams instead of overwriting the file:
Example:
The existing contents of output.log are preserved and new output is appended.
The equivalent traditional form is:
Suppressing Output¶
The special file /dev/null can be used to discard output.
Suppress STDOUT:
Suppress STDERR:
Suppress both:
Or, in Bash:
For example:
Nothing is displayed on the terminal.
This is commonly useful when you only care about whether a command succeeds or fails.
Redirection Order Matters¶
Redirections are processed from left to right.
Consider:
The shell first performs:
Then:
So both go to the file.
Compare this with:
The shell first performs:
At that point STDOUT is still the terminal.
Then:
Therefore STDOUT goes to the file, while STDERR can still go to the terminal.
This difference is important when combining redirections.
Common Redirection Operators¶
| Syntax | Meaning |
|---|---|
> |
Redirect STDOUT and overwrite file |
>> |
Redirect STDOUT and append to file |
2> |
Redirect STDERR and overwrite file |
2>> |
Redirect STDERR and append to file |
1> |
Explicitly redirect STDOUT |
1>> |
Explicitly append STDOUT |
2>&1 |
Redirect STDERR to STDOUT's current destination |
1>&2 |
Redirect STDOUT to STDERR's current destination |
&> |
Redirect STDOUT and STDERR to a file |
&>> |
Append STDOUT and STDERR to a file |
/dev/null |
Discard output |
Practical Logging Example¶
Redirection is frequently used to create simple Bash logs.
#!/bin/bash
LOGFILE="script.log"
echo "Script started" >> "$LOGFILE"
echo "Checking files..." >> "$LOGFILE"
ls /etc/passwd /does-not-exist >> "$LOGFILE" 2>&1
echo "Script finished" >> "$LOGFILE"
The log file can contain:
Script started
Checking files...
/etc/passwd
ls: cannot access '/does-not-exist': No such file or directory
Script finished
Both successful command output and error messages are recorded in the same log.
Practical Example: Separate Output and Errors¶
You can keep normal output and errors in separate files:
View normal output:
View errors:
This separation can be useful when troubleshooting scripts or analyzing command execution.
Key Takeaways¶
-
STDINuses file descriptor0. -
STDOUTuses file descriptor1. -
STDERRuses file descriptor2. -
>redirects STDOUT and overwrites the destination file. -
>>redirects STDOUT and appends to the file. -
2>redirects STDERR. -
2>&1redirects STDERR to the current STDOUT destination. -
1>&2redirects STDOUT to the current STDERR destination. -
&>redirects both STDOUT and STDERR. -
/dev/nullcan be used to discard output. -
Redirection order matters because Bash processes redirections from left to right.
-
> output.log 2>&1is a common pattern for saving both STDOUT and STDERR to the same file.