this post was submitted on 05 Mar 2025
187 points (99.5% liked)

Programmer Humor

34449 readers
104 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] Ephera@lemmy.ml 11 points 2 weeks ago (4 children)

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 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.

[–] barubary@infosec.exchange 11 points 2 weeks ago (2 children)

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.

There isn't. The syntax is

if COMMANDthenCOMMAND(s)...elseCOMMAND(s)...fi

I believe, if you write the then onto the next line, then you don’t need the semicolon.

Yes, but that's true of all commands.

foo; bar; baz

is the same as

foobarbaz

All the ] and -z stuff has nothing to do with if. In your example, the command you're running is literally called [. You're passing it three arguments: -z, "$var", and ]. The ] argument is technically pointless but included for aesthetic reasons to match the opening ] (if you wanted to, you could also write test -z "$var" because [ is just another name for the test command).

Since you can logically negate the exit status of every command (technically, every pipeline) by prefixing a !, you could also write this as:

if ! test "$var"; then ...

The default mode of test (if given one argument) is to check whether it is non-empty.

Now, if you don't want to deal with the vagaries of the test command and do a "native" string check, that would be:

case "$var" in  "") echo "empty";;  *) echo "not empty";;esac
[–] N0x0n@lemmy.ml 7 points 2 weeks ago

My god... I'm so confused by your comment XD ! OP's command is something I already came across, so I somehow got it... But your comment put me in total brain rot !

[–] Damage@feddit.it 5 points 2 weeks ago

Now this is enlightening

[–] balsoft@lemmy.ml 10 points 2 weeks ago* (last edited 2 weeks ago)

Why -z? I have no idea.

From man test (note that [ <expr> ] is just sugar for test <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!

[–] Tangentism@lemmy.ml 3 points 2 weeks ago (2 children)

You could write that as 1 line:

[ -z "$var" ] && echo "empty" || echo "no it aint"

[–] barubary@infosec.exchange 4 points 2 weeks ago

@Tangentism @Ephera Did you mean:

echo "${var:-empty}${var:+no it aint}"

?

[–] AnnaFrankfurter@lemmy.ml 2 points 2 weeks ago

-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