[ 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.

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

status=0
if [ "$status" -eq 0 ]; then
    result="ok"
else
    result="fail"
fi
echo "status:$result"
#!/usr/bin/env bash

status=1
if [ "$status" -eq 0 ]; then
    result="ok"
else
    result="fail"
fi
echo "status:$result"
  1. status ← 0

    3status=04if [ "$status" -eq 0 ]; then
    values this step0status
  2. if [ "$status" -eq 0 ]; then

    3status=04if [ "$status" -eq 0 ]; then5    result="ok"
    values this step0status
  3. result ← ok

    4if [ "$status" -eq 0 ]; then5    result="ok"6else
    values this stepokresult
  4. echo "status:$result"

    8fi9echo "status:$result"
    outputstatus:ok
    values this stepokresult
  1. status ← 1

    3status=14if [ "$status" -eq 0 ]; then
    values this step1status
  2. if [ "$status" -eq 0 ]; then

    3status=14if [ "$status" -eq 0 ]; then5    result="ok"
    values this step1status
  3. result ← fail

    6else7    result="fail"8fi
    values this stepfailresult
  4. echo "status:$result"

    8fi9echo "status:$result"
    outputstatus:fail
    values this stepfailresult
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.