0
0
Bash Scriptingscripting~10 mins

Custom exit codes (exit N) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom exit codes (exit N)
Start Script
Run commands
Call exit N
Script stops
Return exit code N to OS
The script runs commands, then stops immediately when exit N is called, returning N as the exit code to the operating system.
Execution Sample
Bash Scripting
echo "Hello"
exit 3
echo "Bye"
Prints 'Hello', then exits with code 3, so 'Bye' is never printed.
Execution Table
StepCommandActionOutputExit CodeNext Step
1echo "Hello"Print messageHello0Continue
2exit 3Exit script with code 33Stop
3echo "Bye"Skipped because script exitedNo
💡 Script stops at step 2 due to exit 3, so step 3 is not executed.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Exit CodeN/A033
Key Moments - 2 Insights
Why does the script stop after 'exit 3' and not print 'Bye'?
Because 'exit 3' immediately stops the script and returns code 3, so commands after it are not run (see execution_table step 2 and 3).
What does the number after 'exit' mean?
It is the exit code returned to the operating system, showing success (0) or error (non-zero). Here, '3' is a custom code (see variable_tracker).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the exit code after step 1?
A0
B3
C1
DNone
💡 Hint
Check the 'Exit Code' column at step 1 in the execution_table.
At which step does the script stop running commands?
AStep 1
BStep 2
CStep 3
DIt never stops
💡 Hint
Look at the 'Next Step' column in execution_table where it says 'Stop'.
If we change 'exit 3' to 'exit 0', what changes in the exit code?
AExit code becomes 1
BExit code becomes 3
CExit code becomes 0
DNo exit code
💡 Hint
Refer to variable_tracker and understand exit code meaning.
Concept Snapshot
Use 'exit N' in bash to stop a script and return a custom exit code N.
Exit code 0 means success; non-zero means error or special status.
Commands after 'exit' do not run.
Useful for signaling script result to other programs or scripts.
Full Transcript
This example shows a bash script that prints 'Hello', then uses 'exit 3' to stop immediately with exit code 3. The command after exit is skipped. The exit code is a number returned to the operating system to indicate success or failure. Zero means success, any other number is a custom error or status code. This helps other scripts or programs know what happened. The execution table traces each step, showing output and exit code changes. The variable tracker shows how the exit code changes from start to finish. Key moments clarify why the script stops and what the exit code means. The quiz tests understanding of exit codes and script flow.