LOGBOOK

HELP

Quiz Entry - updated: 2026.07.14

What are the string comparison operators in Bash test?

Strings use symbol operators: = (equal), != (not equal), plus -z (empty) and -n (non-empty).

Note the split-brain design: strings use =/!=, numbers use -eq/-ne. Mixing them is a common bug — [ "$a" -eq "$b" ] errors if the values aren't integers. The -z/-n checks are invaluable for validating input: -z "$var" is true when the variable is empty or unset, perfect for "did the user pass an argument?"

Operator Meaning Example
= Equal [ "$a" = "$b" ]
== Equal (same as =) [ "$a" == "$b" ]
!= Not equal [ "$a" != "$b" ]
-z Zero length (empty) [ -z "$str" ]
-n Non-zero length [ -n "$str" ]

Examples:

name="admin"

if [ "$name" = "admin" ]; then
    echo "Welcome admin"
fi

if [ -z "$name" ]; then
    echo "Name is empty"
fi

if [ -n "$name" ]; then
    echo "Name is set: $name"
fi

Always quote variables:

# Correct
[ "$var" = "value" ]

# WRONG - breaks if var is empty
[ $var = "value" ]     # Becomes [ = "value" ] - syntax error!

From Quiz: LIOS / Bash Scripting and Automation | Updated: Jul 14, 2026