Write output of a Bash command to a log file
To write the output of a Bash command to a log file, use redirection operators such as > and >>. Use > when you want to create or overwrite a file. Use >> when you want to append new output at the end of an existing log file.
This is useful in shell scripts where you want to keep a record of command output, timestamps, status messages, or errors for later checking.
Difference between > and >> for Bash log files
Right angle bracket symbol (>) writes the output of a Bash command to a file. If the file is not present, Bash creates it. If the file is already present, its existing content is overwritten.
Double right angle symbol (>>) appends output to a file. If the file is not present, Bash creates it. If the file is already present, new output is added after the existing content.
| Operator | What it does | Common use in logging |
|---|---|---|
> | Creates or overwrites a file | Start a fresh log file at the beginning of a script |
>> | Appends to a file | Add more log lines without deleting previous lines |
2> | Redirects error output only | Save command errors in a log file |
&> | Redirects standard output and errors | Capture complete command output in Bash |
2>&1 | Sends errors to the same destination as output | Log both normal output and errors in portable shell style |
When you are writing to the file for the first time and want no previous data to remain, use >. Later in the same script, use >> to add more lines to the same log file.
Bash script example: write command output to log_file.txt
Following is an example to demonstrate writing output of Bash Commands to Log File.
Bash Script File
#!/bin/bash
log=log_file.txt
# create log file or overrite if already present
printf "Log File - " > $log
# append date to log file
date >> $log
x=$(( 3 + 1 ))
# append some data to log file
echo value of x is $x >> $log
When you run the above bash script, following file would be created with the content specified.
log_file.txt
Log File - Fri Dec 15 11:47:03 IST 2017
value of x is 4
Append multiple Bash command outputs to the same log file
The following script starts with a new log file and then appends several lines. Quoting "$log" is a safer habit because it still works if the file path contains spaces.
#!/bin/bash
log="app.log"
echo "Script started" > "$log"
date >> "$log"
whoami >> "$log"
pwd >> "$log"
echo "Script finished" >> "$log"
If app.log already exists, the first echo command overwrites it. The later commands append to it.
Write both command output and errors to a Bash log file
Many commands produce two kinds of output: standard output and standard error. Normal redirection with > captures only standard output. To capture errors also, redirect standard error.
#!/bin/bash
log="command.log"
ls /tmp /path/that/does/not/exist > "$log" 2>&1
In the command above, > "$log" sends standard output to the file, and 2>&1 sends standard error to the same destination.
Bash also supports the shorter &> form for redirecting both standard output and standard error.
command &> "$log"
To append both standard output and errors instead of overwriting the file, use this form:
command >> "$log" 2>&1
Show Bash command output on console and save it to a log file with tee
Use tee when you want to see command output in the terminal and save the same output to a log file. By default, tee overwrites the file. Add -a to append.
#!/bin/bash
log="build.log"
echo "Build started" | tee "$log"
date | tee -a "$log"
echo "Build completed" | tee -a "$log"
To capture both output and errors while still printing them on the console, redirect errors before passing output to tee.
command 2>&1 | tee -a "$log"
Redirect all output from a Bash script to one log file
Instead of adding >> "$log" to every command, you can redirect all later standard output and errors once using exec. This is useful for longer scripts.
#!/bin/bash
log="script.log"
exec > "$log" 2>&1
echo "Script started"
date
ls /tmp
echo "Script finished"
After the exec line, output from later commands is written to script.log. If you want to append instead of overwrite, use exec >> "$log" 2>&1.
Add timestamps to each Bash log message
For script messages, a small logging function keeps the format consistent. The function below writes a timestamp and message to the same log file.
#!/bin/bash
log="app.log"
write_log() {
printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >> "$log"
}
write_log "Script started"
write_log "Processing input file"
write_log "Script completed"
This pattern is useful when you want each log entry to be easy to scan and sort by time.
Create a log directory before writing Bash output
If the log file is inside a directory that may not exist, create the directory before writing the file. Use mkdir -p so the command does not fail when the directory is already present.
#!/bin/bash
log_dir="logs"
log="$log_dir/app.log"
mkdir -p "$log_dir"
echo "Log file created" > "$log"
Common mistakes while writing Bash output to log files
- Using
>when append is required:>overwrites the file. Use>>to keep previous log lines. - Forgetting to capture errors:
command > log.txtdoes not capture standard error. Use2>&1when you need both output and errors. - Leaving log paths unquoted: prefer
"$log"instead of$log. - Writing to a missing directory: create the log directory first with
mkdir -p. - Overwriting logs during repeated runs: use a timestamped filename or append mode if old logs must be preserved.
Bash log file redirection FAQ
How do I write Bash command output to a file?
Use command > file.txt to write output to a file. This creates the file if needed and overwrites it if it already exists.
How do I append Bash output to an existing log file?
Use command >> logfile.txt. The >> operator adds the command output at the end of the file without removing existing content.
How do I save both Bash output and errors to one log file?
Use command > logfile.txt 2>&1 to overwrite the log file, or command >> logfile.txt 2>&1 to append both output and errors.
How can I show output on the terminal and write it to a log file?
Pipe the command to tee. For example, command | tee logfile.txt writes to the file and also displays output on the console. Use tee -a to append.
How do I redirect an entire Bash script to a log file?
Place exec > "$log" 2>&1 near the top of the script. After that line, later command output and errors are written to the log file.
QA checklist for Bash command output logging tutorial
- Verify that examples clearly distinguish overwrite redirection
>from append redirection>>. - Confirm that examples capturing errors include
2>&1or another explicit standard error redirection. - Check that new Bash examples quote log file variables such as
"$log". - Confirm that
teeexamples mention whether they overwrite or append. - Make sure output-only blocks use the
outputclass, and syntax-only examples uselanguage-bash syntax.
Conclusion: choosing the right Bash log file method
In this Bash Tutorial, we learned how to write the output of a Bash command to a file. Use > for a fresh log, >> for appending, 2>&1 for errors, tee for console plus file output, and exec when an entire script should write to a log file.
TutorialKart.com