Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The -n operator is a unary string evaluation operator in Bash that tests whether a given string has a non-zero length. When executed within a test command construct, it returns an exit status of 0 (true) if the string contains one or more characters, and an exit status of 1 (false) if the string is empty (null) or an unassigned variable. Syntax
[ -n "$VARIABLE" ]
[[ -n "$VARIABLE" ]]
test -n "$VARIABLE"
Evaluation Mechanics
  • Operand: Accepts a single string argument.
  • Return Value: Yields 0 (success/true) for length > 0. Yields 1 (failure/false) for length == 0.
  • Inverse: The -n operator is the exact logical inverse of the -z operator, which tests for a zero-length string.
Parsing and Quoting Nuances When using the POSIX-compliant single bracket [ ] or the test builtin, the string operand must be enclosed in double quotes ("$VARIABLE"). If the variable is unquoted and evaluates to null, the shell performs word splitting before the test command executes. This expands the expression to [ -n ]. In this unquoted scenario, Bash’s parsing rules interpret the isolated -n as a literal non-empty string argument rather than a unary operator, erroneously returning an exit status of 0.

# Incorrect: Evaluates to true (0) even if VAR is empty due to word splitting
[ -n $VAR ] 


# Correct: Safely evaluates to false (1) if VAR is empty
[ -n "$VAR" ] 
When using the Bash-specific double bracket [[ ]] (extended test command), word splitting and pathname expansion are suppressed. Consequently, [[ -n $VAR ]] will correctly evaluate to false if the variable is empty. However, double-quoting the variable remains the standard technical convention to ensure predictable behavior across different shell environments and test constructs.
Master Bash with Deep Grasping Methodology!Learn More