CertGrid CertGrid
Hands-on Lab·Red Hat Certified System Administrator

Shell Conditionals with if and test

One objective, which names if, test and [] explicitly. The single most useful thing to understand is that [ is a command - which explains why it needs spaces around it, why the operators are words like -gt, and why an unquoted empty variable produces a syntax error rather than false.

Shell Scripts Guide 62 of 67 Beginner

[ ] is test and is portable. [[ ]] is a bash keyword with nicer behaviour - no word splitting, && and || inside, pattern matching with =~. Both are acceptable in an answer; [ ] is what the objective names.

Every command on this page runs on RHCSA-A01.
Server NameIP AddressOSRolesCPURAMHDD
RHCSA-A01192.168.0.31RHEL 10.0Practice node (graded) - spare /dev/sda2 Core4 GB50 GB + 15 GB

This guide includes

Use this for one objective that names if, test and [ ] explicitly. This matters because the condition is an exit status rather than a boolean - and unquoted variables break the test in ways that look like logic errors.

Before you start

  1. The tests worth knowing

  2. Exit status is the condition

  3. The quoting that stops it breaking

Official sources