Portable Shell Boundaries
Portable Test
Single-Bracket Conditions
[ is widely available in POSIX-style shells. Simple numeric and string checks can stay portable when a script does not need Bash-only syntax.
Program
Play the script to choose a status value and see the portable test branch.
portable_test.sh
#!/usr/bin/env bash
status=
if [ "$status" -eq 0 ]; then
result="ok"
else
result="fail"
fi
echo "status:$result"
#!/usr/bin/env bash
status=
if [ "$status" -eq 0 ]; then
result="ok"
else
result="fail"
fi
echo "status:$result"
portable test
`[` works in POSIX-style shells for simple comparisons.
numeric comparison
`-eq` compares integer values.
boundary
Use Bash-only `[[ ... ]]` when you need its extra features; otherwise `[` can keep a script easier to port.