Operators and Tests

Turn a specific question into a test, then interpret its status.

Choose a value or a test

Form Job Example
$(( ... )) Insert an integer result "$((count + 1))"
(( ... )) Return arithmetic truth status (( count == 5 ))
[[ ... ]] Bash string/pattern/file condition [[ "$label" == host-* ]]
[ ... ] Test command with separate arguments [ "$count" -eq 5 ]

Zero status means success. [ ... ] needs spaces and a closing ]; quote expanded data. Arithmetic input here is trusted fixture data.

Value and status are different

count=0
(( count ))
status=$?
printf 'zero expression status=%s\n' "$status"

Expected: zero expression status=1.

An arithmetic zero is false. A command status of zero means success. Save $? before another command replaces it.

Test the boundary

used=8
limit=8
(( used >= limit )); printf 'at-or-over=%s\n' "$?"
(( used > limit )); printf 'strictly-over=%s\n' "$?"

Expected: at-or-over=0, then strictly-over=1.

Choose a comparison that matches the policy at the boundary.

Literal text or a pattern?

label=host-east
[[ "$label" == 'host-*' ]]; printf 'literal=%s\n' "$?"
[[ "$label" == host-* ]]; printf 'pattern=%s\n' "$?"

Expected: literal=1, then pattern=0.

The quoted right-hand operand is literal; the unquoted operand is a shell pattern. Numeric magnitude needs numeric comparison.

Existence does not answer every question

Test Question
-e Does the target exist?
-f Is the target a regular file?
-d Is it a directory?
-s Does the file have nonzero size?
-L Is the named entry a symbolic link?

An empty regular file passes -f and fails -s. A dangling link passes -L and fails -e.

Access belongs to a process and environment

-r, -w, and -x ask about current access. -O asks about ownership by the effective user.

A current-user test does not establish another user's access. Privilege, ACLs, mount options, and directory access can affect the operation.

Practice: Why is a privileged process a poor stand-in for an ordinary student when testing a mode-000 file?

Express the needed preconditions

Use the lesson's complete setup to create input.txt and reports.

[ -f input.txt ] && [ -r input.txt ] && [ -d reports ]
status=$?
printf 'valid=%s\n' "$status"

Expected with the fixture: valid=0.

A later operation can still fail or observe a changed path. Preflight is an observation, not a lock or proof of content trust.

Short-circuit logic has consequences

true && false || printf 'fallback ran after second-command failure\n'

The fallback runs even though the first command succeeded.

a && b || c does not express a general two-branch decision. Chapter 8 develops if for that purpose.

Consolidate on your VM

Then complete the graded lab.

Create valid, missing, empty, and linked inputs. Record the property each test observes and its actual status. Explain what the evidence cannot establish.

Use synthetic data. Do not source or execute an input merely because a file test passed.

Source context: Linuxize file tests and Bash articles linked in the lessons.