String null space error: binary operator expected

When using - Z or - N to judge a variable empty, it should be noted that if you use the form of [- N ${Arg}] directly, if there are spaces in ${Arg} it will report an error

#!/bin/bash
ARG="sd dd"
if [ -n ${ARG} ]; then
    echo 'ARG:' ${ARG}
else
    echo 'ARG is empty.'
fi

Output:

line 27: [: sd: binary operator expected
ARG is empty.

Password> [- N ${Arg}] password> [- n “${Arg}]

Similar Posts: