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
#!/usr/bin/env bash
has_awk=
if [ "$has_awk" -eq 1 ]; then
runner="awk"
else
runner="sed"
fi
plan="$runner:extract-field"
echo "$plan"
#!/usr/bin/env bash
has_awk=
if [ "$has_awk" -eq 1 ]; then
runner="awk"
else
runner="sed"
fi
plan="$runner:extract-field"
echo "$plan"
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.