Bash Script Debugging and Error Handling Essentials Quiz

Assess your grasp of debugging techniques and error handling strategies in Bash scripting with this quiz. Learn to identify, analyze, and resolve common errors to make your scripts more robust and reliable.

  1. Enabling Debug Mode

    Which option enables Bash to display each command and its arguments as they are executed while running a script?

    1. -x
    2. -z
    3. -n
    4. -e

    Explanation: The '-x' option causes Bash to print each command and its arguments as they run, which is helpful for debugging. The '-e' option makes the script exit on any error, but does not display commands. The '-n' option only checks script syntax without executing commands, and '-z' is not a valid debug flag. Only '-x' reveals command execution for detailed tracing.

  2. Syntax Error Identification

    In a Bash script, what type of error does a missing 'fi' at the end of an 'if' statement cause?

    1. Logical Error
    2. Runtime Error
    3. Resource Error
    4. Syntax Error

    Explanation: A missing 'fi' leads to a syntax error because Bash cannot properly interpret the script structure. Runtime errors happen during script execution, logical errors stem from mistakes in program logic, and resource errors involve unavailable system resources. Only syntax errors are triggered by missing reserved words like 'fi'.

  3. Checking Last Command Exit Status

    What is the special Bash variable used to check the exit status of the last executed command?

    1. $?
    2. $$
    3. $@
    4. $#

    Explanation: The variable '$?' holds the exit status of the most recent command, which is essential for error checking. '$$' contains the script's process ID, '$#' gives the number of arguments passed, and '$@' holds all positional parameters. Only '$?' is used for checking command success or failure.

  4. Script Exit on Error

    Which line at the top of a Bash script causes it to exit immediately if any command fails?

    1. set -x
    2. set +e
    3. set -e
    4. set -o debug

    Explanation: 'set -e' makes Bash exit the script if a command returns a non-zero status. 'set -x' enables debug output, not error response, 'set -o debug' is not a valid setting, and 'set +e' disables exit-on-error behavior. 'set -e' is the appropriate choice for automatic script termination on errors.

  5. Using 'trap' for Cleanup

    Which command structure ensures a cleanup function runs when the script receives an interrupt signal?

    1. signal cleanup_function
    2. catch cleanup_function
    3. onexit cleanup_function
    4. trap cleanup_function INT

    Explanation: The 'trap' command is used to specify a function to execute when the script gets a specific signal, such as INT (interrupt). There are no 'signal', 'catch', or 'onexit' commands in Bash for this purpose. Only 'trap' correctly sets up signal handling for cleanup routines.

  6. Syntax Checking Only

    What does running 'bash -n script.sh' do with your Bash script?

    1. Executes the script
    2. Checks for syntax errors only
    3. Prints each line before executing
    4. Exits on the first error

    Explanation: The '-n' flag tells Bash to read the script and check for syntax errors without actually executing any commands. Executing the script requires no such flag, printing each line is done using '-x', and '-e' exits on the first error. Only '-n' performs syntax checking.

  7. Redirecting Error Messages

    Which redirection sends standard error messages to a file named errors.log?

    1. 2u003E errors.log
    2. erroru003E errors.log
    3. u003E errors.log
    4. 1u003E errors.log

    Explanation: The '2u003E' operator redirects file descriptor 2 (standard error) to the specified file. '1u003E' and 'u003E' redirect standard output, not error. 'erroru003E' is not a recognized operator in Bash. Only '2u003E' correctly handles error message redirection.

  8. Uninitialized Variables

    What is likely to happen if you use an unset variable in Bash without quotes and strict settings?

    1. It produces a syntax error
    2. The script immediately exits
    3. It throws a segmentation fault
    4. It expands to an empty string

    Explanation: By default, an unset variable expands to an empty string in Bash, which may cause subtle bugs. Syntax errors occur if there are structural mistakes, not from uninitialized variables. Immediate exit happens only if 'set -u' is enabled, and segmentation faults do not occur in interpreted scripts. An empty string expansion is the typical behavior.

  9. Common Debug Output

    Which command is typically used in Bash scripts to display the value of variables for debugging purposes?

    1. echo
    2. copy
    3. remove
    4. touch

    Explanation: 'echo' outputs the value of variables, making it useful for tracking execution flow. 'copy' and 'remove' are not Bash commands, and 'touch' only creates empty files. Only 'echo' is meant for displaying variable values on the terminal.

  10. Locating the Line of Error

    If you see 'line 12: unexpected EOF while looking for matching', what does 'line 12' refer to?

    1. A comment line
    2. The last executed command
    3. The line where the error was detected
    4. The start of the script

    Explanation: 'line 12' points to the location in the script where Bash encountered the error. It does not refer to the beginning of the script or a comment, and it may not be directly related to the last executed command. This helps you quickly find and address syntax mistakes.

  11. Option for Pipe Failure

    Which command ensures a Bash script exits if any command in a pipeline fails?

    1. set -o pipefail
    2. set -x
    3. set -u
    4. set -o allfail

    Explanation: 'set -o pipefail' forces the script to consider any failed command in a pipeline as a failure for the whole pipe. 'set -x' only traces execution, 'set -u' exits on use of unset variables, and 'set -o allfail' is not a valid option. Only 'set -o pipefail' targets pipeline failures.

  12. Preventing Accidental File Overwrite

    Which option disables overwriting existing files with output redirection unless forced?

    1. set -f
    2. set -c
    3. set -o nodup
    4. set -o noclobber

    Explanation: 'set -o noclobber' (or 'set -C') prevents redirection from overwriting files unless 'u003E' is replaced with 'u003E|'. 'set -c' is not a recognized Bash option. 'set -o nodup' does not exist, and 'set -f' disables pathname expansion, not file overwrite prevention. 'noclobber' is specifically for safe file handling.

  13. Handling Errors in Functions

    What is the typical way to return an error from a Bash function?

    1. Use the return statement with a non-zero value
    2. Use 'echo error'
    3. Use 'break'
    4. Use 'exit' with zero

    Explanation: Returning a non-zero value with 'return' inside a function signals an error. 'break' is for loops, not functions. Exiting with zero indicates successful completion, and 'echo error' only prints text, without affecting exit status. Only 'return' properly communicates errors from functions.

  14. Detecting an Unset Variable at Runtime

    Which setting in Bash causes the script to exit when it encounters an attempt to use an unset variable?

    1. set -a
    2. set -u
    3. set -e
    4. set -o nounset

    Explanation: 'set -u' makes Bash exit with an error when an unset variable is referenced. 'set -e' is for exiting on command errors, 'set -a' automatically marks variables for export, and 'set -o nounset' is synonymous with 'set -u' but not typically given as an answer in shorthand form. Only 'set -u' triggers this strict variable checking.

  15. Finding the Source of a Command Not Found Error

    If you receive 'command not found' when running a script, what is the most likely cause?

    1. A typo in the command name
    2. Running out of disk space
    3. Permission denied on the script
    4. Unmatched quotes

    Explanation: 'Command not found' usually results from mistyping a command name or the command not being installed. Disk space, permissions, or unmatched quotes typically produce different error messages such as 'no space left' or 'permission denied.' Only a typo or missing command triggers 'command not found.'

  16. Graceful Script Termination

    Which command is used in Bash to terminate a script immediately and optionally specify an exit status?

    1. quit
    2. exit
    3. stop
    4. leave

    Explanation: 'exit' stops script execution immediately and can take an optional exit status code for success or error purposes. 'leave', 'quit', and 'stop' are not valid Bash commands for ending scripts. 'exit' is the standard, portable way to terminate execution gracefully.