Challenge - 5 Problems
Exit Code Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:00remaining
What is the exit code after a successful command?
Consider the following Bash commands run in sequence:
echo "Hello World" echo $?What will be the output of the second echo command?
Bash Scripting
echo "Hello World"
echo $?Attempts:
2 left
💡 Hint
The exit code $? shows if the last command succeeded or failed. Zero means success.
✗ Incorrect
The first echo command runs successfully and prints "Hello World". The exit code $? after a successful command is 0.
💻 Command Output
intermediate1:00remaining
Exit code after a failing command
What will be the output of this script?
ls /nonexistent_directory echo $?
Bash Scripting
ls /nonexistent_directory echo $?
Attempts:
2 left
💡 Hint
When a command fails, the exit code is non-zero. Different commands may use different codes.
✗ Incorrect
The ls command fails because the directory does not exist. On many systems, ls returns exit code 2 for this error.
📝 Syntax
advanced1:30remaining
Which script correctly checks the exit code?
You want to run a command and then check if it succeeded using its exit code. Which script snippet correctly does this?
Attempts:
2 left
💡 Hint
Remember to have spaces around brackets and use correct syntax for if statements in Bash.
✗ Incorrect
Option D uses correct syntax: spaces around [ and ], the -eq operator, and the then keyword on the same line or next line.
🚀 Application
advanced1:30remaining
What is the exit code after a pipeline?
Consider this pipeline:
grep 'pattern' file.txt | sort echo $?What does the exit code represent?
Bash Scripting
grep 'pattern' file.txt | sort
echo $?Attempts:
2 left
💡 Hint
In Bash, $? after a pipeline returns the exit code of the last command by default.
✗ Incorrect
By default, $? returns the exit code of the last command in the pipeline, which is sort here.
🧠 Conceptual
expert2:00remaining
How to get exit codes of all commands in a pipeline?
You run a pipeline of commands in Bash:
How can you get the exit codes of all three commands?
cmd1 | cmd2 | cmd3
How can you get the exit codes of all three commands?
Attempts:
2 left
💡 Hint
Bash stores exit codes of all pipeline commands in a special array variable.
✗ Incorrect
The special array ${PIPESTATUS[@]} holds exit codes of all commands in the last pipeline executed.