[Bash] Test whether a string is non-empty

Created on November 30, 2026

Use -n when a statement should run only if a string contains at least one character. Here, the conditional prints a message for either result.

NAME="Taro"
if [ -n "$NAME" ]; then
  echo "NAME is not empty"
else
  echo "NAME is empty"
fi

Output

NAME is not empty

See testing a non-empty string with if -n for an example without an else branch.

Bash Shell command