Why -z? I have no idea. I will also routinely forget the ]; then part. I believe, if you write the then onto the next line, then you don’t need the semicolon. And then someone’s probably gonna tell me to use double-brackets [[ ]] instead, which probably does something.
Arguably, I never fully learned Bash syntax, but it also is just a stupid if-statement. There shouldn’t be that much complexity in it.
-z means zero length and mostly [[ ]] are used when you want to add multiple conditions. But there are also few test cases which are only in bash so they also need double brackets
Here’s an example, I have looked up many times (like just now), which checks whether a string is empty:
var="" if [ -z "$var" ]; then echo "empty" else echo "not empty" fi
Why
-z
? I have no idea. I will also routinely forget the]; then
part. I believe, if you write thethen
onto the next line, then you don’t need the semicolon. And then someone’s probably gonna tell me to use double-brackets[[ ]]
instead, which probably does something.Arguably, I never fully learned Bash syntax, but it also is just a stupid if-statement. There shouldn’t be that much complexity in it.
From
man test
(note that[ <expr> ]
is just sugar fortest <expr>
):-n STRING the length of STRING is nonzero -z STRING the length of STRING is zero
So,
-z
stands for Zero.Hope this helps you remember it!
-z
means zero length and mostly[[ ]]
are used when you want to add multiple conditions. But there are also few test cases which are only in bash so they also need double bracketsYou could write that as 1 line:
[ -z "$var" ] && echo "empty" || echo "no it aint"