this post was submitted on 22 Sep 2025
22 points (92.3% liked)

Linux

58560 readers
954 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 6 years ago
MODERATORS
 

Hi,

I trying to test two condition together (AND) under bash but it's not working...

The goal is ti have True when two variables are either not set or empty (empty string)

I've tried

if [[ -n VARIABLE1 && -n VARIABLE2 ]]; then
    echo "OK"
fi

Here I get the "OK" no matter what .

Thanks.

top 16 comments
sorted by: hot top controversial new old
[–] dion_starfire@sh.itjust.works 3 points 4 days ago

You're probably wanting [ -z "${VAR1}" -a -z "${VAR2}" ]. Note in bash that there are minor differences in how [ ] and [[ ]] tests are handled. You can pull up a handy cheat sheet of the operands on most distros by running man test, though you'll need to read through the CONDITIONAL EXPRESSIONS section of man bash if you want to see the minor differences of the single vs double square bracket commands (mostly whether locale applies to string order, as well as whether operands are evaluated in numeric comparisons).

[–] BillibusMaximus@sh.itjust.works 8 points 6 days ago (1 children)

If you want true for empty strings, you want -z not -n

if [[ -z "VARIABLE1"  && -z "VARIABLE2"  ]]; then
    echo "OK"
fi
[–] Farnsworth@lemmy.world 2 points 4 days ago

in [[ there is generally no need for quoting, except the right hand side of an = comparison

[–] DeuxChevaux@lemmy.world 7 points 6 days ago (2 children)

[ condition1 ] && [ condition2 ] && echo "Good" || echo "Bad"

[–] Farnsworth@lemmy.world 5 points 4 days ago* (last edited 4 days ago)

Never use a && b || c. It is not the same as if a; then b; else c; fi: when a succeeds but b fails, it will run both b and c.

I would not bother with [ unless you absolutely need compatibility with non-bash shells.

[–] suff@piefed.social 14 points 6 days ago* (last edited 6 days ago)

Explanation

[ is an alias for the program test, so you can call man test for more info.
&& is bash syntax for conjunction. In A && B, B will only be called if A returned a exit code >0 (error). You can call man bash for more info.
|| is bash syntax for disjunction. In A || B, B will only be called if A returned exit code =0 (success). true and false are programs that just return exit codes 0 respectively 1.

[–] harsh3466@lemmy.ml 7 points 6 days ago (1 children)

To check for an empty string, use -z. -n checks to see if a string is not empty.

[–] Farnsworth@lemmy.world 1 points 4 days ago* (last edited 4 days ago)

in [[, empty strings are falsy, so this also works:

[[ ! $VARIABLE1 && ! $VARIABLE2 ]] && echo "OK"
[–] Rick_C137@programming.dev 3 points 5 days ago* (last edited 5 days ago) (2 children)

Thank you all for yours input

What finally did work

if [[ -z VARIABLE1 && -z VARIABLE2 ]]; then
    echo "OK"
fi

If only Linux was using Python syntax that would be so much more intuitive...

[–] sping@lemmy.sdf.org 9 points 4 days ago

Linux does use Python syntax... in Python.

In Bash though, it uses Bash syntax.

[–] Farnsworth@lemmy.world 2 points 4 days ago* (last edited 4 days ago)

The variables need a dollar sign: $VARIABLE1

help test shows what -n and -z do.

[–] kittenroar@beehaw.org 3 points 6 days ago (1 children)

You need to reference the value of the variable, ie:

if [[ -n "$VARIABLE1" && -n "$VARIABLE2" ]]; then
    echo "OK"
fi
[–] Rick_C137@programming.dev 2 points 5 days ago (1 children)

not working, both variables do not exist and the echo "OK" do not trigger.

[–] kittenroar@beehaw.org 2 points 5 days ago* (last edited 5 days ago)

Then it is working. That is what that code was checking for.

Specifically, -n checks if the variable exists and also does not have a null value.

If you want to reverse it, ie, check that those conditions are not true, put an exclamation mark in front of the whole thing.

[–] thingsiplay@beehaw.org 2 points 6 days ago

Try this:

#!/usr/bin/env bash

a=""
if [[ -z "${a}" && -z "${b}" ]]; then
    echo "OK"
else
    echo "Not OK"
fi

a="OK"
if [[ -n "${a}" && -z "${b}" ]]; then
    echo "More ${a}"
else
    echo "More Unokay"
fi
[–] fubarx@lemmy.world 1 points 6 days ago

Could try:

if [ condition1 ] && [ condition2 ]; then
  echo "OK"
fi