-1

This question already has an answer here:

bash>BRANCH_NAME="My/Test/Branch/Name"
bash>if [ -n "${BRANCH_NAME}" ]; then echo ${BRANCH_NAME}; else echo "latest"; fi

Result

bash>"My/Test/Branch/Name"

What is the purpose of -n and where can I find some documentation on it? I am guessing that the flag is checking for a null or empty string.

marked as duplicate by muru, Rui F Ribeiro, msp9011, Michael Homer bash May 20 at 9:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    The opposite: for not-empty string. -z is for empty string. – muru May 17 at 9:24
  • 3
    It may look odd, but the if construct evaluates the result of a command, and [ is a command. Knowing this, in Bash you can try help [, which will tell you that [ is a synonym of test, then... – fra-san May 17 at 9:31
  • @muru I dont think that this is a duplicate of the other question mentioned. This question is much more narrowly focused and specifically asking about -n and getting an answer specific to the question. – robbie70 May 17 at 10:34
4

It's described in the man page under CONDITIONAL EXPRESSIONS:

string
-n string
             True if the length of string is non-zero.

Not the answer you're looking for? Browse other questions tagged or ask your own question.