0
0
Bash Scriptingscripting~20 mins

Custom exit codes (exit N) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exit Code Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the exit code of this script?
Consider this bash script:
#!/bin/bash
exit 42

What will be the exit code when this script finishes?
Bash Scripting
#!/bin/bash
exit 42
A42
B0
C1
D255
Attempts:
2 left
💡 Hint
The exit command returns the number you give it as the exit code.
💻 Command Output
intermediate
1:30remaining
What is the exit code after this command?
You run this command in bash:
exit 300

What will be the actual exit code returned?
Bash Scripting
exit 300
A44
B300
C255
D0
Attempts:
2 left
💡 Hint
Exit codes are limited to 0-255 and wrap around using modulo 256.
🔧 Debug
advanced
2:00remaining
Why does this script always exit with code 0?
Look at this script:
#!/bin/bash
false
exit 0

Why does it always exit with code 0 even though 'false' failed?
Bash Scripting
#!/bin/bash
false
exit 0
ABecause the shell ignores exit codes from commands.
BBecause 'false' actually returns 0.
CBecause exit codes are always 0 unless an error is caught.
DBecause the script explicitly exits with 0, ignoring previous commands.
Attempts:
2 left
💡 Hint
The last exit command sets the script's exit code.
🧠 Conceptual
advanced
1:00remaining
What is the valid range for exit codes in bash?
Which of these is the correct range for exit codes in bash scripts?
A-128 to 127
B0 to 1024
C0 to 255
DAny integer value
Attempts:
2 left
💡 Hint
Exit codes are stored in a single byte.
🚀 Application
expert
2:00remaining
What is the exit code of this script?
Consider this bash script:
#!/bin/bash
if false; then
  exit 1
else
  exit 256
fi

What exit code will the script return when run?
Bash Scripting
#!/bin/bash
if false; then
  exit 1
else
  exit 256
fi
A1
B0
C256
D9
Attempts:
2 left
💡 Hint
256 modulo 256 is 0.