Bird
Raised Fist0
Bash Scriptingscripting~5 mins

Custom exit codes (exit N) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does exit N do in a bash script?

exit N stops the script and sends the number N as the exit code to the system. This code tells if the script ended successfully or with an error.

Click to reveal answer
beginner
Why use custom exit codes instead of just exit 0 or exit 1?
<p>Custom exit codes let you show different reasons for failure or success. For example, <code>exit 2</code> might mean "file not found," while <code>exit 3</code> means "permission denied." This helps other scripts or users understand what happened.</p>
Click to reveal answer
beginner
What is the valid range for exit codes in bash scripts?

Exit codes must be between 0 and 255. 0 means success, and any number from 1 to 255 means different errors or statuses.

Click to reveal answer
beginner
How can you check the exit code of the last command in bash?

Use the special variable $?. It holds the exit code of the last command or script run.

Click to reveal answer
beginner
Example: What exit code will this script return?
#!/bin/bash
if [ ! -f myfile.txt ]; then
  exit 2
else
  exit 0
fi

If myfile.txt does not exist, the script exits with code 2. Otherwise, it exits with 0 meaning success.

Click to reveal answer
What does exit 0 usually mean in a bash script?
AThe script ran successfully
BThe script failed
CThe script is still running
DThe script has a syntax error
Which exit code range is valid in bash scripts?
AAny positive number
B1 to 100
C0 to 1000
D0 to 255
How do you get the exit code of the last command in bash?
A$!
B$?
C$#
D$$
If a script ends with exit 3, what does it mean?
AThe script succeeded
BThe script is running in background
CThe script failed with a specific error
DThe script is paused
What happens if you use exit 300 in a bash script?
AIt exits with code 44 (300 modulo 256)
BIt exits with code 300
CIt causes an error and script stops immediately
DIt exits with code 0
Explain how and why you would use custom exit codes in a bash script.
Think about how scripts tell the system if they succeeded or failed.
You got /4 concepts.
    Describe how to check the exit code of the last command and why it is useful.
    This is how you know if a command worked before moving on.
    You got /4 concepts.

      Practice

      (1/5)
      1. What does the command exit 0 mean in a bash script?
      easy
      A. The script will continue running after this command.
      B. The script encountered an error and stopped.
      C. The script finished successfully without errors.
      D. The script restarts from the beginning.

      Solution

      1. Step 1: Understand exit codes in bash

        In bash, exit 0 means the script ended without any errors.
      2. Step 2: Interpret the meaning of 0

        Exit code 0 always signals success to the system or calling program.
      3. Final Answer:

        The script finished successfully without errors. -> Option C
      4. Quick Check:

        exit 0 = success [OK]
      Hint: Remember: 0 means success, anything else means error [OK]
      Common Mistakes:
      • Thinking exit 0 means error
      • Assuming script continues after exit
      • Confusing exit 0 with restart
      2. Which of the following is the correct syntax to exit a bash script with code 5?
      easy
      A. exit 5
      B. exit code 5
      C. exit(5)
      D. exit -5

      Solution

      1. Step 1: Recall bash exit syntax

        The correct way to exit with a code is exit N, where N is a number.
      2. Step 2: Check each option

        exit 5 uses exit 5, which is valid syntax. Others are invalid in bash.
      3. Final Answer:

        exit 5 -> Option A
      4. Quick Check:

        exit N syntax = exit 5 [OK]
      Hint: Use just 'exit' followed by a number, no extra words [OK]
      Common Mistakes:
      • Adding 'code' after exit
      • Using parentheses like a function
      • Using negative numbers for exit
      3. What will be the exit code of this script?
      #!/bin/bash
      if [ -f "/tmp/file.txt" ]; then
        exit 3
      else
        exit 0
      fi
      medium
      A. 0 if /tmp/file.txt exists, 3 if not
      B. 3 if /tmp/file.txt exists, 0 if not
      C. Always 0
      D. Always 3

      Solution

      1. Step 1: Understand the if condition

        The script checks if the file /tmp/file.txt exists using -f.
      2. Step 2: Analyze exit codes for each case

        If the file exists, it exits with code 3; otherwise, it exits with 0.
      3. Final Answer:

        3 if /tmp/file.txt exists, 0 if not -> Option B
      4. Quick Check:

        File exists -> exit 3; else -> exit 0 [OK]
      Hint: Match exit code to condition true/false carefully [OK]
      Common Mistakes:
      • Mixing up the if and else branches
      • Assuming exit 0 always means file exists
      • Ignoring the file check condition
      4. Identify the error in this script snippet:
      #!/bin/bash
      exit 256
      medium
      A. Exit code 256 is invalid; exit codes must be 0-255.
      B. Missing quotes around 256.
      C. exit command should be lowercase.
      D. exit requires a string, not a number.

      Solution

      1. Step 1: Check valid exit code range

        Bash exit codes must be between 0 and 255 inclusive.
      2. Step 2: Analyze the given exit code

        256 is outside this range, so it is invalid and will wrap around or cause unexpected behavior.
      3. Final Answer:

        Exit code 256 is invalid; exit codes must be 0-255. -> Option A
      4. Quick Check:

        Exit codes range 0-255 [OK]
      Hint: Exit codes must be 0-255, no higher numbers [OK]
      Common Mistakes:
      • Using numbers above 255
      • Thinking quotes are needed
      • Confusing case sensitivity of exit
      5. You want a script to exit with code 10 if a user is not root, and 0 if root. Which snippet correctly implements this?
      hard
      A. if [ "$USER" != "root" ]; then exit 10; else exit 0; fi
      B. if [ "$UID" -eq 0 ]; then exit 10; else exit 0; fi
      C. if [ "$USER" == "root" ]; then exit 10; else exit 0; fi
      D. if [ "$UID" -ne 0 ]; then exit 10; else exit 0; fi

      Solution

      1. Step 1: Identify how to check root user

        Root user has UID 0, so checking $UID -ne 0 means user is not root.
      2. Step 2: Match exit codes to condition

        If user is not root, exit 10; else exit 0 for root user.
      3. Final Answer:

        if [ "$UID" -ne 0 ]; then exit 10; else exit 0; fi -> Option D
      4. Quick Check:

        UID 0 means root; non-zero means not root [OK]
      Hint: Use UID 0 to check root, not USER string [OK]
      Common Mistakes:
      • Comparing USER string instead of UID
      • Reversing the exit codes
      • Using == instead of -eq for numbers
      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