Recall & Review
beginner
What is the purpose of error logging in bash scripts?
Error logging helps capture and record errors during script execution. It makes troubleshooting easier by showing what went wrong and where.
Click to reveal answer
beginner
How do you redirect error messages to a log file in bash?
Use the syntax
command > logfile 2>&1. This sends both standard output and error output to the log file.Click to reveal answer
intermediate
What does
2>&1 mean in bash redirection?It means redirect standard error (file descriptor 2) to the same place as standard output (file descriptor 1).
Click to reveal answer
intermediate
Why use a function for error logging in bash scripts?
A function centralizes error handling. It makes the script cleaner and easier to maintain by reusing the same logging format and actions.
Click to reveal answer
intermediate
Show a simple bash function that logs errors with timestamps.
Example:
log_error() {
echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: $1" >&2
}
This prints the error message with a timestamp to standard error.Click to reveal answer
Which bash redirection sends both output and errors to the same file?
✗ Incorrect
Option A redirects standard output to logfile and then redirects standard error (2) to the same place as standard output (1).
What does the following bash code do?
exec 2>error.log✗ Incorrect
The exec command changes the file descriptor 2 (standard error) to write into error.log for the rest of the script.
Why is it helpful to include timestamps in error logs?
✗ Incorrect
Timestamps help track when errors occurred, which is useful for debugging and correlating events.
Which of these is a good practice for error logging in bash scripts?
✗ Incorrect
Clear messages with timestamps make error logs useful for troubleshooting.
What does this function do?
log_error() {
echo "ERROR: $1" >&2
}✗ Incorrect
The function prints the message passed as $1 to standard error using >&2.
Explain how to redirect error messages to a log file in a bash script and why it is useful.
Think about how to capture errors and save them for later review.
You got /4 concepts.
Describe how you would create a reusable error logging function in bash and what features it should have.
Consider how to make error messages clear and easy to find.
You got /4 concepts.