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.
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.
-z string
: True if the string is null (an empty string)
See https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions
-n
is the opposite of -z
. if [ -n "${1}" ]
passes if the string is not null and not empty. –
Malcommalcontent -n
redundant with the default behavior of if [ $2 ]
or are there some differences? –
Wallace -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.
The expression -z string
is true if the length of string is zero
.
if [[ -z "${YOUR_ENV_VAR}" ]]
it will get interpolated to if [[ -z "" ]]
at runtime when that env var is empty, thus resulting in a logical true, which is what you want. –
Mellar © 2022 - 2024 — McMap. All rights reserved.