Forrest logo
back to the [[ tool

shell:warp:1242c

Check if string length is non-zero
$ [[ -z ${string} ]]
try on your machine

The command [[ -z ${string} ]] is a conditional statement used in scripting, typically in shell or bash scripting.

Here's the breakdown of each element in this command:

  • [[ and ]] are used to enclose the conditional expression. The double square brackets indicate that this is a conditional statement, similar to the single square brackets '[ ]' used with the 'test' command. The double square brackets offer more flexibility and improved syntax for conditionals.

  • -z is an option that checks whether the given variable or string is empty. It stands for "zero length" or "zero size".

  • ${string} is a variable representing a string or any value that you want to check for emptiness. It can be replaced with an actual string value or a variable containing a string.

Now, let's understand how this command works:

The command [[ -z ${string} ]] is used to check if the variable or string represented by ${string} is empty. If the content of ${string} is empty (has zero length or size), the expression evaluates to true, and the command returns an exit status of 0. Otherwise, if ${string} has any value, the expression evaluates to false and the command returns an exit status of 1.

This conditional statement is often used for flow control in scripting. For example, you can use it in an "if" statement like this:

if [[ -z ${string} ]]; then
   echo "String is empty!"
else
   echo "String is not empty!"
fi
This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the [[ tool