J O S E P H J O S E P H

Bash String Test Operators

String Test Operators

=, ==

Return true if strings are equal. Double-equals is bash-specific and not supported in other shells.

if [[ "str1" = "str2" ]]; then echo "Strings are equal"; fi

-z

Return true if string is null (has a zero length). Always quote the tested string.

STR=""
if [[ -z "$STR" ]]; then echo "String is null"; fi

-n

Return true if string is not null. Always quote the tested string.

STR="foo"
if [[ -n "$STR" ]]; then echo "String is not null"; fi