What does -z mean in Bash?

Bash

Bash Problem Overview


I'm looking at the following code:

if [ -z $2 ]; then
        echo "usage: ...

(The 3 dots are irrelevant usage details.)
Maybe I'm googling it wrong, but I couldn't find an explanation for the -z option.

Bash Solutions


Solution 1 - Bash

-z string True if the string is null (an empty string)

Solution 2 - Bash

-z

string is null, that is, has zero length

String=''   # Zero-length ("null") string variable.

if [ -z "$String" ]
then
  echo "\$String is null."
else
  echo "\$String is NOT null."
fi     # $String is null.

Solution 3 - Bash

test -z returns true if the parameter is empty (see man sh or man test).

Solution 4 - Bash

The expression -z string is true if the length of string is zero.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNoichView Question on Stackoverflow
Solution 1 - BashYu HaoView Answer on Stackoverflow
Solution 2 - Bashuser2659589View Answer on Stackoverflow
Solution 3 - BashknittlView Answer on Stackoverflow
Solution 4 - BashmohitView Answer on Stackoverflow