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?
Acommand > logfile 2>&1
Bcommand 2> logfile
Ccommand > logfile
Dcommand 1> logfile 1>&2
✗ 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
ARedirects standard output to error.log
BRedirects all future error messages to error.log
CDeletes error.log
DRuns error.log as a command
✗ 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?
ATo confuse users
BTo make logs bigger
CTo speed up the script
DTo know when errors happened
✗ 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?
ALogging errors with clear messages and timestamps
BIgnoring errors silently
CPrinting errors only to the screen without saving
DUsing random file names for logs
✗ Incorrect
Clear messages with timestamps make error logs useful for troubleshooting.
What does this function do?
log_error() {
echo "ERROR: $1" >&2
}
ADeletes error messages
BPrints an error message to standard output
CPrints an error message to standard error
DLogs errors to a file automatically
✗ 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.
Practice
(1/5)
1. What does the 2> operator do in a bash script?
easy
A. Redirects error messages to a file
B. Redirects standard output to a file
C. Appends output to a file
D. Runs a command in the background
Solution
Step 1: Understand bash output streams
Bash has two main output streams: standard output (1) and standard error (2).
Step 2: Identify the meaning of 2>
The operator 2> redirects the standard error stream to a file or location.
Final Answer:
Redirects error messages to a file -> Option A
Quick Check:
2> means error redirect [OK]
Hint: Remember 2 is error stream, so 2> redirects errors [OK]
Common Mistakes:
Confusing 2> with > which redirects standard output
Thinking 2> appends instead of overwriting
Assuming 2> runs commands in background
2. Which of the following is the correct syntax to redirect only error messages (overwriting the file) to a file named error.log?
easy
A. command > error.log
B. command 2>> error.log
C. command &> error.log
D. command 2> error.log
Solution
Step 1: Understand redirection operators
2> redirects standard error and overwrites the file; 2>> appends errors.
Step 2: Match requirement to syntax
We want to redirect only errors and overwrite the file, so 2> error.log is correct.
Final Answer:
command 2> error.log -> Option D
Quick Check:
Use 2> for error overwrite [OK]
Hint: Use 2> to overwrite error file, 2>> to append [OK]
Common Mistakes:
Using > which redirects standard output, not errors
Using &> which redirects both output and errors
Confusing append (>>) with overwrite (>)
3. What will be the output of the following command if script.sh prints "Success" to standard output and "Error occurred" to standard error?
./script.sh > output.log 2> error.log
medium
A. "Success" is saved in output.log, "Error occurred" in error.log
B. Both messages saved in output.log
C. Both messages appear on the terminal
D. "Error occurred" saved in output.log, "Success" in error.log
Solution
Step 1: Analyze redirection operators
> output.log redirects standard output to output.log, 2> error.log redirects standard error to error.log.
Step 2: Match outputs to files
"Success" goes to standard output, so saved in output.log; "Error occurred" goes to standard error, saved in error.log.
Final Answer:
"Success" is saved in output.log, "Error occurred" in error.log -> Option A
Quick Check:
Separate output and error files [OK]
Hint: Use > for output, 2> for errors to separate logs [OK]
Common Mistakes:
Assuming both outputs go to same file
Confusing 2> with >
Expecting output on terminal despite redirection
4. Identify the error in this command intended to redirect errors to errors.log:
command 2>2 errors.log
medium
A. Should use &> instead of 2>
B. Missing space between command and 2>
C. Extra '2' causes errors to be redirected to file named '2' instead of 'errors.log'
D. No error, command is correct
Solution
Step 1: Examine the redirection syntax
The correct syntax to redirect errors is 2> filename. Here, 2>2 errors.log redirects stderr to file 2, treating errors.log as a command argument.
Step 2: Identify the cause of error
The extra '2' makes the filename 2 instead of errors.log.
Final Answer:
Extra '2' causes errors to be redirected to file named '2' instead of 'errors.log' -> Option C
Quick Check:
2> must be followed by correct filename [OK]
Hint: Check redirection target filename after 2> [OK]
Common Mistakes:
Adding extra numbers or symbols in filename position
Confusing &> with 2>
Missing spaces causing parsing errors
5. You want to run a script and save both standard output and error messages into separate files, but also keep the error messages appended (not overwritten). Which command achieves this?
hard
A. ./script.sh > output.log 2> error.log
B. ./script.sh > output.log 2>> error.log
C. ./script.sh &> output.log
D. ./script.sh >> output.log 2> error.log
Solution
Step 1: Understand output and error redirection
> overwrites standard output, 2>> appends standard error to a file.
Step 2: Match requirement to command
We want output overwritten in output.log and errors appended in error.log, so ./script.sh > output.log 2>> error.log fits.
Final Answer:
./script.sh > output.log 2>> error.log -> Option B
Quick Check:
Use 2>> to append errors [OK]
Hint: Use 2>> to append errors, > to overwrite output [OK]
Common Mistakes:
Using 2> which overwrites error file
Using &> which merges output and errors
Appending output but overwriting errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.