Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Error logging patterns in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Error logging patterns
Start Script
↓
Run Command
↓
Check Exit Status
↓
Log Error
↓
End Script
The script runs a command, checks if it failed, logs an error if so, then continues or ends.
Execution Sample
Bash Scripting
command_that_might_fail
if [ $? -ne 0 ]; then
  echo "Error: command failed" >> error.log
fi
Runs a command, checks if it failed, and logs an error message to a file if it did.
Execution Table
StepActionCommand Exit Status ($?)ConditionLog ActionOutput
1Run command_that_might_fail11 != 0 (True)Append error message to error.logerror.log updated with 'Error: command failed'
2Check condition again1No further actionNo logNo change
3End script---Script ends
💡 Command exit status is 1 (failure), so error is logged and script ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
$?-111
error.logempty or previous contentadded error messageunchangedcontains error message
Key Moments - 3 Insights
Why do we check if $? is not zero?
Because $? holds the exit status of the last command; zero means success, non-zero means failure. See execution_table step 1 where $? is 1, triggering the error log.
Does the error message overwrite or append to the log file?
It appends using >>, so previous logs stay. This is shown in execution_table step 1 where the message is added to error.log.
What happens if the command succeeds?
If $? is 0, the condition is false and no error is logged. The script continues without changes to error.log, as shown by the absence of logging in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $? after running the command?
A0
B-1
C1
DUndefined
💡 Hint
Check the 'Command Exit Status ($?)' column in step 1 of the execution_table.
At which step is the error message added to error.log?
AStep 1
BStep 2
CStep 3
DNo step logs error
💡 Hint
Look at the 'Log Action' column in the execution_table.
If the command exit status was 0, how would the execution_table change?
AError message would still be logged
BNo error message would be logged
CScript would exit immediately
DCommand would run again
💡 Hint
Refer to the 'Condition' and 'Log Action' columns in the execution_table for when $? equals 0.
Concept Snapshot
Error logging in bash:
Run a command
Check exit status with $? (0=success, non-zero=failure)
If failure, append error message to a log file
Use '>>' to keep previous logs
This helps track failures without stopping script
Full Transcript
This visual execution shows how a bash script runs a command, checks its exit status using $?, and logs an error message to a file if the command fails. The flow starts with running the command, then checking if $? is not zero, which means failure. If failed, the script appends an error message to error.log. Variables tracked include $? and the contents of error.log. Key points include understanding $? exit codes, appending logs instead of overwriting, and no logging on success. The quiz tests understanding of exit status, logging step, and behavior on success.

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

  1. Step 1: Understand bash output streams

    Bash has two main output streams: standard output (1) and standard error (2).
  2. Step 2: Identify the meaning of 2>

    The operator 2> redirects the standard error stream to a file or location.
  3. Final Answer:

    Redirects error messages to a file -> Option A
  4. 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

  1. Step 1: Understand redirection operators

    2> redirects standard error and overwrites the file; 2>> appends errors.
  2. Step 2: Match requirement to syntax

    We want to redirect only errors and overwrite the file, so 2> error.log is correct.
  3. Final Answer:

    command 2> error.log -> Option D
  4. 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

  1. Step 1: Analyze redirection operators

    > output.log redirects standard output to output.log, 2> error.log redirects standard error to error.log.
  2. 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.
  3. Final Answer:

    "Success" is saved in output.log, "Error occurred" in error.log -> Option A
  4. 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

  1. 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.
  2. Step 2: Identify the cause of error

    The extra '2' makes the filename 2 instead of errors.log.
  3. Final Answer:

    Extra '2' causes errors to be redirected to file named '2' instead of 'errors.log' -> Option C
  4. 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

  1. Step 1: Understand output and error redirection

    > overwrites standard output, 2>> appends standard error to a file.
  2. 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.
  3. Final Answer:

    ./script.sh > output.log 2>> error.log -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard