Shell scripts use exit status values to decide whether later work should continue. Modeling the status keeps the lesson deterministic without exiting the replay.

Program

Play the script to choose a status and see the next-step decision.

status
exit_status_check.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

status=0
command="validate-config"
if [ "$status" -eq 0 ]; then
    result="continue"
else
    result="stop:$status"
fi
echo "$command:$result"
#!/usr/bin/env bash

status=2
command="validate-config"
if [ "$status" -eq 0 ]; then
    result="continue"
else
    result="stop:$status"
fi
echo "$command:$result"
  1. status ← 0

    3status=04command="validate-config"
    values this step0status
  2. command ← validate-config

    3status=04command="validate-config"5if [ "$status" -eq 0 ]; then
    values this stepvalidate-configcommand
  3. if [ "$status" -eq 0 ]; then

    4command="validate-config"5if [ "$status" -eq 0 ]; then6    result="continue"
    values this step0status
  4. result ← continue

    5if [ "$status" -eq 0 ]; then6    result="continue"7else
    values this stepcontinueresult
  5. echo "$command:$result"

    9fi10echo "$command:$result"
    outputvalidate-config:continue
    values this stepvalidate-configcommandcontinueresult
  1. status ← 2

    3status=24command="validate-config"
    values this step2status
  2. command ← validate-config

    3status=24command="validate-config"5if [ "$status" -eq 0 ]; then
    values this stepvalidate-configcommand
  3. if [ "$status" -eq 0 ]; then

    4command="validate-config"5if [ "$status" -eq 0 ]; then6    result="continue"
    values this step2status
  4. result ← stop:2

    7else8    result="stop:$status"9fi
    values this stepstop:2result2status
  5. echo "$command:$result"

    9fi10echo "$command:$result"
    outputvalidate-config:stop:2
    values this stepvalidate-configcommandstop:2result
exit status An exit status is the numeric result of a command.
zero Status zero conventionally means success.
stop path A nonzero status should usually stop or recover before continuing.