Challenge - 5 Problems
Set -e Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output when using set -e with a failing command?
Consider this bash script:
What will be printed when this script runs?
#!/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"
Attempts:
2 left
💡 Hint
Remember, set -e stops the script if any command fails.
✗ Incorrect
The script prints "Start" then runs the command 'false' which returns an error. Because of 'set -e', the script exits immediately and does not print "End".
💻 Command Output
intermediate2:00remaining
How does set -e behave inside a function?
Given this script:
What will be the output?
#!/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"
Attempts:
2 left
💡 Hint
set -e exits the whole script if any command fails, even inside functions.
✗ Incorrect
The script prints "Before function" then calls test_func which prints "Inside function". The 'false' command fails, so due to 'set -e', the script exits immediately. "After false" and "After function" are not printed.
🔧 Debug
advanced2:00remaining
Why does set -e not exit on a failing command in a pipeline?
Look at this script:
Why does it print "Still running" even though 'false' fails?
#!/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"
Attempts:
2 left
💡 Hint
Check which command's exit status is checked in a pipeline with set -e.
✗ Incorrect
In bash, with set -e, only the exit status of the last command in a pipeline is considered. Here, 'true' is last and succeeds, so the script continues.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
Look for the option that enables pipefail with set -e.
✗ Incorrect
To make set -e exit on any failing command in a pipeline, you must enable 'pipefail'. Option D correctly sets both 'set -e' and 'set -o pipefail'.
🚀 Application
expert2: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?
Attempts:
2 left
💡 Hint
Use a way to ignore error from one command without disabling set -e globally.
✗ Incorrect
Option C uses 'command2 || true' which makes the command always succeed, so set -e does not exit. This is a common pattern to ignore errors from a single command while keeping set -e active.