Portable scripts often check whether a preferred command is available and keep a simpler fallback path for restricted environments.

Program

Play the script to choose whether the preferred tool is available and see the selected runner.

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

has_awk=1
if [ "$has_awk" -eq 1 ]; then
    runner="awk"
else
    runner="sed"
fi
plan="$runner:extract-field"
echo "$plan"
#!/usr/bin/env bash

has_awk=0
if [ "$has_awk" -eq 1 ]; then
    runner="awk"
else
    runner="sed"
fi
plan="$runner:extract-field"
echo "$plan"
  1. has_awk ← 1

    3has_awk=14if [ "$has_awk" -eq 1 ]; then
    values this step1has_awk
  2. if [ "$has_awk" -eq 1 ]; then

    3has_awk=14if [ "$has_awk" -eq 1 ]; then5    runner="awk"
    values this step1has_awk
  3. runner ← awk

    4if [ "$has_awk" -eq 1 ]; then5    runner="awk"6else
    values this stepawkrunner
  4. plan ← awk:extract-field

    8fi9plan="$runner:extract-field"10echo "$plan"
    values this stepawk:extract-fieldplanawkrunner
  5. echo "$plan"

    9plan="$runner:extract-field"10echo "$plan"
    outputawk:extract-field
    values this stepawk:extract-fieldplan
  1. has_awk ← 0

    3has_awk=04if [ "$has_awk" -eq 1 ]; then
    values this step0has_awk
  2. if [ "$has_awk" -eq 1 ]; then

    3has_awk=04if [ "$has_awk" -eq 1 ]; then5    runner="awk"
    values this step0has_awk
  3. runner ← sed

    6else7    runner="sed"8fi
    values this stepsedrunner
  4. plan ← sed:extract-field

    8fi9plan="$runner:extract-field"10echo "$plan"
    values this stepsed:extract-fieldplansedrunner
  5. echo "$plan"

    9plan="$runner:extract-field"10echo "$plan"
    outputsed:extract-field
    values this stepsed:extract-fieldplan
command check Real scripts commonly use `command -v tool` to detect optional tools.
fallback A fallback keeps useful behavior when the preferred command is absent.
plan first Building a command plan is deterministic and easy to test before adding side effects.