0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.