Every command exits with a numeric status. Scripts can test that status to choose the next action.

Program

Play the script to search fixed text for a selected word and label the result.

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

data=$'ok\nwarn\nfail'
needle="ok"
if printf '%s\n' "$data" | grep -q "$needle"; then
    label="found"
else
    label="missing"
fi
echo "$needle:$label"
#!/usr/bin/env bash

data=$'ok\nwarn\nfail'
needle="warn"
if printf '%s\n' "$data" | grep -q "$needle"; then
    label="found"
else
    label="missing"
fi
echo "$needle:$label"
#!/usr/bin/env bash

data=$'ok\nwarn\nfail'
needle="missing"
if printf '%s\n' "$data" | grep -q "$needle"; then
    label="found"
else
    label="missing"
fi
echo "$needle:$label"
  1. data ← ok, warn, fail

    3data=$'ok\nwarn\nfail'4needle="ok"
    values this stepok, warn, faildata
  2. needle ← ok

    3data=$'ok\nwarn\nfail'4needle="ok"5if printf '%s\n' "$data" | grep -q "$needle"; then
    values this stepokneedle
  3. grep status ← 0

    4needle="ok"5if printf '%s\n' "$data" | grep -q "$needle"; then6    label="found"
    values this step0grep statusokneedle
  4. label ← found

    5if printf '%s\n' "$data" | grep -q "$needle"; then6    label="found"7else
    values this stepfoundlabel
  5. echo "$needle:$label"

    9fi10echo "$needle:$label"
    outputok:found
    values this stepokneedlefoundlabel
  1. data ← ok, warn, fail

    3data=$'ok\nwarn\nfail'4needle="warn"
    values this stepok, warn, faildata
  2. needle ← warn

    3data=$'ok\nwarn\nfail'4needle="warn"5if printf '%s\n' "$data" | grep -q "$needle"; then
    values this stepwarnneedle
  3. grep status ← 0

    4needle="warn"5if printf '%s\n' "$data" | grep -q "$needle"; then6    label="found"
    values this step0grep statuswarnneedle
  4. label ← found

    5if printf '%s\n' "$data" | grep -q "$needle"; then6    label="found"7else
    values this stepfoundlabel
  5. echo "$needle:$label"

    9fi10echo "$needle:$label"
    outputwarn:found
    values this stepwarnneedlefoundlabel
  1. data ← ok, warn, fail

    3data=$'ok\nwarn\nfail'4needle="missing"
    values this stepok, warn, faildata
  2. needle ← missing

    3data=$'ok\nwarn\nfail'4needle="missing"5if printf '%s\n' "$data" | grep -q "$needle"; then
    values this stepmissingneedle
  3. grep status ← 1

    4needle="missing"5if printf '%s\n' "$data" | grep -q "$needle"; then6    label="found"
    values this step1grep statusmissingneedle
  4. label ← missing

    7else8    label="missing"9fi
    values this stepmissinglabel
  5. echo "$needle:$label"

    9fi10echo "$needle:$label"
    outputmissing:missing
    values this stepmissingneedlemissinglabel

Follow the Status

  1. data contains ok, warn, and fail.
  2. needle starts as ok.
  3. grep -q "$needle" finds ok, so its status is 0.
  4. The then branch sets label to found.
  5. The script prints ok:found. | needle | grep status | label | output | | --- | --- | --- | --- | | ok | 0 | found | ok:found | | warn | 0 | found | warn:found | | missing | non-zero | missing | missing:missing |
exit status Status `0` means success; non-zero means the command did not succeed.
if command `if command; then` branches using the command's exit status.
grep -q `grep -q` searches quietly and reports the result through its status.

Exercise: command_status.sh

Reproduce ok:found, then use the pinned needle variants warn and missing to predict warn:found and missing:missing.