Portable Shell Boundaries
Command Fallback
Pick an Available Tool
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.
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"
has_awk ← 1
3has_awk=14if [ "$has_awk" -eq 1 ]; thenvalues this step1has_awkif [ "$has_awk" -eq 1 ]; then
3has_awk=14if [ "$has_awk" -eq 1 ]; then5 runner="awk"values this step1has_awkrunner ← awk
4if [ "$has_awk" -eq 1 ]; then5 runner="awk"6elsevalues this stepawkrunnerplan ← awk:extract-field
8fi9plan="$runner:extract-field"10echo "$plan"values this stepawk:extract-fieldplanawkrunnerecho "$plan"
9plan="$runner:extract-field"10echo "$plan"outputawk:extract-fieldvalues this stepawk:extract-fieldplan
has_awk ← 0
3has_awk=04if [ "$has_awk" -eq 1 ]; thenvalues this step0has_awkif [ "$has_awk" -eq 1 ]; then
3has_awk=04if [ "$has_awk" -eq 1 ]; then5 runner="awk"values this step0has_awkrunner ← sed
6else7 runner="sed"8fivalues this stepsedrunnerplan ← sed:extract-field
8fi9plan="$runner:extract-field"10echo "$plan"values this stepsed:extract-fieldplansedrunnerecho "$plan"
9plan="$runner:extract-field"10echo "$plan"outputsed:extract-fieldvalues 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.