Bash Debugging and Error Handling Fundamentals Quiz Quiz

Dive into essential Bash script debugging and error handling techniques with these beginner-friendly questions. Enhance your command-line proficiency by identifying common mistakes, understanding debug commands, and learning best practices for reliable scripting.

  1. Identifying Syntax Errors

    What happens if you forget the closing 'fi' in a Bash if-statement such as 'if [ $x -eq 1 ]; then echo Match;'?

    1. The script will skip the if condition
    2. The script will ignore the missing 'fi'
    3. The script will run successfully
    4. The script shows a syntax error and stops executing

    Explanation: Bash requires every opened if block to be closed with a corresponding 'fi'; forgetting 'fi' leads to a clear syntax error and script termination. 'The script will run successfully' is incorrect as the syntax is incomplete. 'The script will ignore the missing 'fi'' is wrong because Bash does not automatically correct structural mistakes. 'Skipping the if condition' is not possible since Bash enforces proper block structure.

  2. Setting Pipefail for Debugging

    Which Bash option ensures a pipeline returns a failure status if any command fails in a pipeline like 'cmd1 | cmd2'?

    1. set -v
    2. set +x
    3. set -e
    4. set -o pipefail

    Explanation: 'set -o pipefail' causes a pipeline to fail if any command fails, which is important for robust error handling. 'set -e' exits the script on any non-zero exit code but does not address pipelines specifically. 'set +x' disables debugging output and does not affect error status. 'set -v' prints shell input lines but does not help with pipeline failures.

  3. Enabling Trace Output

    What effect does running a script with 'bash -x script.sh' have while debugging your Bash script?

    1. It prints each command as they are executed
    2. It comments all output lines
    3. It stops the script if any variable is unset
    4. It prompts for confirmation before every command

    Explanation: Running with 'bash -x' provides a trace of each command before execution, making it easier to see the flow and locate errors. It does not stop the script for unset variables, which would require 'set -u'. Commenting or prompting for confirmation before every command are not features of the '-x' flag.

  4. Handling Unset Variables

    If you use 'set -u' at the start of your Bash script, what behavior should you expect when referencing an unset variable like $UNSET_VAR?

    1. The variable is treated as an empty string
    2. The script skips that line
    3. The variable is set to zero automatically
    4. The script exits with an error

    Explanation: 'set -u' configures Bash to treat the use of unset variables as an error, terminating the script. Treating it as an empty string is the default behavior without 'set -u'. Bash never sets unset variables to zero by default, nor does it skip lines containing unset variables.

  5. Error Handling with Exit Codes

    What is the recommended way to check if the last executed command in a Bash script failed?

    1. $O
    2. exitstatus
    3. $?
    4. $#

    Explanation: The special variable '$?' always holds the exit status of the last command, which is a standard way to check for failures. '$O' and 'exitstatus' do not exist as Bash variables. '$#' gives the number of positional parameters, not the exit status.

  6. Using 'trap' for Cleanup

    Which construct allows a Bash script to execute a cleanup function automatically when it terminates, such as removing a temporary file?

    1. trap cleanup EXIT
    2. finally cleanup
    3. try cleanup END
    4. onexit cleanup

    Explanation: 'trap cleanup EXIT' sets up a trap so that the 'cleanup' function is run when the script exits. 'try' and 'finally' are not Bash concepts. 'onexit' is not a valid Bash statement either, making 'trap' the only correct answer here.

  7. Diagnosing Quoting Errors

    What is a likely effect if you forget to quote variables in a Bash script, for example writing 'rm $file' when $file contains spaces?

    1. The script automatically adds the quotes
    2. The command is ignored completely
    3. The variable is always treated as one argument
    4. The command may fail or delete unintended files

    Explanation: Omitting quotes around variables containing spaces can cause Bash to split values, leading to possible failure or deletion of the wrong files. The shell does not treat such variables as single arguments without quotes. Ignoring the command or automatically quoting are incorrect, as Bash executes the statement as written.

  8. Redirecting Error Output

    If you want to save only error messages from a command 'mycmd' into a file called error.log, which command should you use?

    1. mycmd u003Eu003E error.log
    2. mycmd u003E error.log
    3. mycmd 1u003E error.log
    4. mycmd 2u003E error.log

    Explanation: The '2u003E' syntax redirects standard error to a file, capturing error messages specifically. Using 'u003E' or '1u003E' only redirects standard output, not errors. 'u003Eu003E' appends output, but without '2u003Eu003E', it only affects standard output—not standard error.

  9. Testing File Existence

    Which Bash expression is used to check if a file named 'file.txt' exists before attempting to read it?

    1. [ -f file.txt ]
    2. { file.txt }
    3. [ -z file.txt ]
    4. [ eq file.txt ]

    Explanation: '[ -f file.txt ]' tests if 'file.txt' exists and is a regular file. '[ -z file.txt ]' checks if the string is empty, not file existence. '{ file.txt }' is not a valid Bash condition. '[ eq file.txt ]' does not conform to Bash test syntax.

  10. Debugger for Interactive Debugging

    Which built-in Bash tool allows step-by-step interactive debugging of scripts at the command line?

    1. tail
    2. bashdb
    3. chmod
    4. grep

    Explanation: 'bashdb' is a debugger that provides interactive, step-by-step inspection of Bash scripts. 'grep' searches files for patterns, 'tail' displays the end of files, and 'chmod' changes file permissions—none of which provide interactive script debugging features.