[Bash] Test for a non-empty string with if -n

Created on November 29, 2026

In an if condition such as [ -n "$greeting" ], -n tests whether the string has at least one character. The following example runs echo because greeting contains Hello World.

greeting='Hello World'
if [ -n "$greeting" ]; then
    echo "$greeting"
fi
Hello World
Bash Shell command