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

Written against the versions above. `[ ]` 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.0 (Coughlan)Practice node (graded) - spare /dev/sda2 Core4 GB50 GB + 15 GB

Before you start

  1. The tests worth knowing

  2. Exit status is the condition

  3. The quoting that stops it breaking

Official sources