Automation, Orchestration, and Scripting

In a Bash script, which test returns exit status 0 both when LOGDIR is set to an empty string and when LOGDIR is unset?

Show the answer

Correct answerD. [ -z "$LOGDIR" ]

AWhy not this: -n succeeds when the string is not empty, so it returns 1 in both of these cases.

BWhy not this: -d tests whether a directory exists; an empty path is not a directory, so it returns 1.

CWhy not this: -v succeeds when the variable is set, even to an empty string, so it returns 1 when LOGDIR is unset.

DWhy this is right: -z succeeds for a zero-length string, and a quoted unset variable expands to an empty string.

The -z operator succeeds when the length of the string is zero, and a quoted unset variable expands to an empty string. It does not test file existence or distinguish unset from empty.