Bird
Raised Fist0
Bash Scriptingscripting~20 mins

set -e for exit on error in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Set -e Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output when using set -e with a failing command?
Consider this bash script:
#!/bin/bash
set -e
echo "Start"
false
echo "End"

What will be printed when this script runs?
Bash Scripting
#!/bin/bash
set -e
echo "Start"
false
echo "End"
AStart
B
Start
End
CEnd
DNo output
Attempts:
2 left
💡 Hint
Remember, set -e stops the script if any command fails.
💻 Command Output
intermediate
2:00remaining
How does set -e behave inside a function?
Given this script:
#!/bin/bash
set -e
function test_func() {
  echo "Inside function"
  false
  echo "After false"
}

echo "Before function"
test_func
echo "After function"

What will be the output?
Bash Scripting
#!/bin/bash
set -e
function test_func() {
  echo "Inside function"
  false
  echo "After false"
}

echo "Before function"
test_func
echo "After function"
A
Before function
Inside function
After function
B
Before function
Inside function
C
Before function
Inside function
After false
After function
D
Inside function
After false
After function
Attempts:
2 left
💡 Hint
set -e exits the whole script if any command fails, even inside functions.
🔧 Debug
advanced
2:00remaining
Why does set -e not exit on a failing command in a pipeline?
Look at this script:
#!/bin/bash
set -e
false | true
echo "Still running"

Why does it print "Still running" even though 'false' fails?
Bash Scripting
#!/bin/bash
set -e
false | true
echo "Still running"
ABecause only the last command's exit status in a pipeline affects set -e
BBecause set -e is disabled inside pipelines
CBecause 'true' overrides the failure of 'false' in set -e
DBecause set -e ignores failures in all pipelines
Attempts:
2 left
💡 Hint
Check which command's exit status is checked in a pipeline with set -e.
📝 Syntax
advanced
2:00remaining
Which script correctly uses set -e to stop on errors and handle a failing command in a pipeline?
You want the script to exit if any command fails, including in pipelines. Which script does this correctly?
A
#!/bin/bash
set -e
set +o pipefail
false | true
echo "Done"
B
#!/bin/bash
set -eo pipefail
false | true
echo "Done"
C
#!/bin/bash
set -e
false | true
echo "Done"
D
#!/bin/bash
set -e
set -o pipefail
false | true
echo "Done"
Attempts:
2 left
💡 Hint
Look for the option that enables pipefail with set -e.
🚀 Application
expert
2:00remaining
How to safely run multiple commands with set -e and ignore errors in one command?
You want a script that stops on any error except for one command that might fail but should not stop the script. Which snippet does this correctly?
A
#!/bin/bash
set -e
command1
set +e
command2
set -e
command3
B
#!/bin/bash
set -e
command1
if ! command2; then echo "Ignore error"; fi
command3
C
#!/bin/bash
set -e
command1
command2 || true
command3
D
#!/bin/bash
set -e
command1
command2
command3
Attempts:
2 left
💡 Hint
Use a way to ignore error from one command without disabling set -e globally.

Practice

(1/5)
1. What does the set -e command do in a bash script?
easy
A. It stops the script immediately if any command returns a non-zero exit status.
B. It prints all commands before executing them.
C. It ignores errors and continues running the script.
D. It runs the script in debug mode.

Solution

  1. Step 1: Understand the purpose of set -e

    The set -e option tells bash to exit the script if any command fails (returns a non-zero status).
  2. Step 2: Compare with other options

    Options like printing commands or ignoring errors do not match set -e's behavior.
  3. Final Answer:

    It stops the script immediately if any command returns a non-zero exit status. -> Option A
  4. Quick Check:

    set -e = exit on error [OK]
Hint: Remember: set -e means exit on any error [OK]
Common Mistakes:
  • Confusing set -e with set -x (debug mode)
  • Thinking it ignores errors instead of stopping
  • Assuming it prints commands before running
2. Which of the following is the correct way to enable exit on error in a bash script?
easy
A. set -e
B. set +e
C. exit -e
D. enable -e

Solution

  1. Step 1: Recall the syntax for enabling exit on error

    The correct syntax to enable exit on error is set -e.
  2. Step 2: Eliminate incorrect options

    set +e disables exit on error, exit -e and enable -e are invalid commands.
  3. Final Answer:

    set -e -> Option A
  4. Quick Check:

    Enable exit on error = set -e [OK]
Hint: Use set -e to turn on exit on error [OK]
Common Mistakes:
  • Using set +e which disables exit on error
  • Confusing exit command with set
  • Typing invalid commands like enable -e
3. Consider this script with set -e enabled:
#!/bin/bash
set -e
echo "Start"
false
echo "End"
What will be the output when this script runs?
medium
A. Start End
B. No output
C. End
D. Start

Solution

  1. Step 1: Analyze script commands with set -e

    The script prints "Start", then runs false which returns a non-zero exit status causing the script to exit immediately.
  2. Step 2: Determine which commands run

    Because of set -e, the script stops after false, so "End" is never printed.
  3. Final Answer:

    Start -> Option D
  4. Quick Check:

    set -e stops script on error, so only "Start" prints [OK]
Hint: Script stops at first error, so later lines don't run [OK]
Common Mistakes:
  • Expecting all echo statements to run
  • Ignoring that false returns error
  • Thinking set -e allows script to continue
4. You have this script:
#!/bin/bash
set -e
echo "Start"
ls /nonexistent_directory
echo "End"
The script exits immediately after the ls command. How can you fix it to continue even if ls fails?
medium
A. Remove set -e from the script.
B. Add || true after the ls command.
C. Change ls to ls -e.
D. Add set +e at the start of the script.

Solution

  1. Step 1: Understand why script exits

    set -e causes the script to exit on any command failure, so ls /nonexistent_directory failing stops the script.
  2. Step 2: Use || true to ignore error

    Appending || true makes the command always succeed, preventing exit.
  3. Final Answer:

    Add || true after the ls command. -> Option B
  4. Quick Check:

    Use command || true to bypass set -e exit [OK]
Hint: Add || true to ignore errors on specific commands [OK]
Common Mistakes:
  • Removing set -e loses error protection
  • Using invalid ls -e option
  • Trying set +e disables exit globally, not selective
5. You want a bash script to stop on any error except for a specific command that might fail but should not stop the script. How do you write this script snippet correctly?
#!/bin/bash
set -e
# Your command here
other_command
Which snippet correctly allows your_command to fail without stopping the script?
hard
A. your_command ; true other_command
B. set +e your_command other_command
C. your_command || true other_command
D. trap '' ERR your_command other_command

Solution

  1. Step 1: Understand selective error ignoring

    To allow one command to fail without stopping the script under set -e, append || true to that command.
  2. Step 2: Compare other options

    your_command ; true other_command: your_command ; true fails because set -e exits after your_command before true. set +e your_command other_command disables set -e but doesn't re-enable it for other_command. trap '' ERR your_command other_command ignores ERR trap but doesn't restore it, affecting other_command.
  3. Final Answer:

    your_command || true other_command -> Option C
  4. Quick Check:

    Use command || true to ignore errors selectively [OK]
Hint: Add || true after command to ignore its errors [OK]
Common Mistakes:
  • Disabling set -e globally instead of locally
  • Using if without handling exit status properly
  • Using traps unnecessarily complicates script
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