0
0
Linux CLIscripting~20 mins

Shell options (set -e, set -x) in Linux CLI - Practice Problems & Coding Challenges

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

What will be printed when this script runs?
Linux CLI
#!/bin/bash
set -e
echo "Start"
false
echo "End"
AEnd
B
Start
End
CNo output
DStart
Attempts:
2 left
💡 Hint
Remember, set -e stops the script if a command fails.
💻 Command Output
intermediate
2:00remaining
What does set -x do in this script?
Given this script:
#!/bin/bash
set -x
VAR=5
echo $((VAR + 3))

What will be the output?
Linux CLI
#!/bin/bash
set -x
VAR=5
echo $((VAR + 3))
A8
B
+ VAR=5
+ echo 8
8
C
VAR=5
8
DNo output
Attempts:
2 left
💡 Hint
set -x shows each command before running it.
📝 Syntax
advanced
2:00remaining
Which script will stop immediately on any error?
Select the script that uses shell options correctly to stop execution immediately if any command fails.
A
#!/bin/bash
set -e
false
echo "This will not print"
B
#!/bin/bash
set -o errexit
false
echo "This will print"
C
#!/bin/bash
set -x
false
echo "This will not print"
D
#!/bin/bash
set +e
false
echo "This will not print"
Attempts:
2 left
💡 Hint
Look for the option that stops on errors.
💻 Command Output
advanced
2:00remaining
What error does this script produce without set -e?
Given this script:
#!/bin/bash
echo "Start"
ls /nonexistent_directory
echo "End"

What will be the output?
Linux CLI
#!/bin/bash
echo "Start"
ls /nonexistent_directory
echo "End"
A
Start
ls: cannot access '/nonexistent_directory': No such file or directory
End
B
Start
End
C
Start
ls: cannot access '/nonexistent_directory': No such file or directory
DNo output
Attempts:
2 left
💡 Hint
Without set -e, script continues after errors.
🚀 Application
expert
2:00remaining
How to debug a failing script with set -x and stop on error with set -e?
You have a script that sometimes fails silently. You want to see each command as it runs and stop immediately if any command fails. Which option combination should you use at the start of your script?
Aset -e -x +v
Bset +e +x
Cset -ex
Dset -x; set +e
Attempts:
2 left
💡 Hint
Combine options to both debug and stop on errors.