Skip to main content
2 of 2
added 129 characters in body
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

On Unix systems, utilities return a zero exit-status if what they did went well, if it succeeded without errors etc. If a utility fails, it returns a non-zero exit status.

This way, it's possible to tell a user with the exit-status what when wrong (see e.g. the "EXIT VALUES" or similar section of some manuals, for example the manual of rsync). In short, if the operation succeeded, the exit-status is zero and if it's not zero, it failed and the exit-status may say why it failed.

The test utility, which you use, follow this pattern in that if a comparison succeeded (it's "true"), it returns zero. If the comparison (or whatever operation it carries out) fails (it's "false"), it returns a non-zero exit-status.

The if keyword takes a utility and acts on its exit-status. If the utility returns a zero exit-status, it takes the default branch, otherwise it takes the else branch:

if test 4 -eq 3; then
    echo 4 is 3
else
    echo 4 is not 3
fi

if can take any utility:

if grep -q 'secret' file.txt; then
    echo file.txt contains secret
else
    echo file.txt does not contain secret
fi
if mkdir mydir; then
    echo created directory mydir
else
    echo failed to create directory mydir
fi
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k