64

I want to check, if multiple variable are set or not, if set then only execute the script code, otherwise exit.

something like:

if [ ! $DB=="" && $HOST=="" && $DATE==""  ]; then
  echo "you did not set any variable"
   exit 1;
else
  echo "You are good to go"
fi      
2
  • for check variable has value or not [ -z $a -a -z $b -a -z $c ] && echo "has not value" || echo " ok good to go!" Commented Mar 26, 2015 at 13:14
  • You need spaces around operators. [[ $HOST=="" ]] is always true, because it's parsed as [[ -n "$HOST==" ]] Commented Apr 17, 2024 at 1:42

7 Answers 7

122

You can use -z to test whether a variable is unset or empty:

if [[ -z $DB || -z $HOST || -z $DATE ]]; then
  echo 'one or more variables are undefined'
  exit 1
fi

echo "You are good to go"

As you have used the tag, I've used an extended test [[, which means that I don't need to use quotes around my variables. I'm assuming that you need all three variables to be defined in order to continue. The exit in the if branch means that the else is superfluous.

The standard way to do it in any POSIX-compliant shell would be like this:

if [ -z "$DB" ] || [ -z "$HOST" ] || [ -z "$DATE" ]; then
  echo 'one or more variables are undefined'        
  exit 1
fi

The important differences here are that each variable check goes inside a separate test and that double quotes are used around each parameter expansion.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I guess you could also use -o to combine the tests in a single predicate, rather than using || and three separate [ calls?
@BeeOnRope The recommendation is to avoid using -a and -o (see the Application Usage section), especially when dealing with variables that could be user-defined.
Thanks for mentioning the "quotes" difference. I didn't even see that diff in my code :(
22

If you are ok with writing a function for this purpose, it can be pretty convenient.

This solution uses the ${!VAR_NAME} syntax to check whether the variable is empty and has the added benefit of telling you which variable names are empty.

check_vars()
{
    var_names=("$@")
    for var_name in "${var_names[@]}"; do
        [ -z "${!var_name}" ] && echo "$var_name is unset." && var_unset=true
    done
    [ -n "$var_unset" ] && exit 1
    return 0
}

# Usage for this case
check_vars DB HOST DATE
echo "You are good to go" 

Comments

5

I wound up using variable-variables to loop through an easily managed HEREDOC list of variable names:

# Ensure non-empty values.
# Loop through HEREDOC, test variable-variable isn't blank.
while read var; do
  [ -z "${!var}" ] && { echo "$var is empty or not set. Exiting.."; exit 1; }
done << EOF
KUBE_NAMESPACE
DOCKER_REGISTRY
DOCKER_DEPLOY_USER
DOCKER_DEPLOY_PASSWORD
DOCKER_DEPLOY_EMAIL
EOF

Comments

1

You can check it also by put the variables name in a file

DB=myDB
HOST=myDB
DATE=myDATE

then test them if currently empty or unset

#!/bin/bash
while read -r line; do
    var=`echo $line | cut -d '=' -f1`
    test=$(echo $var)
    if [ -z "$(test)" ]; then 
        echo 'one or more variables are undefined'
        exit 1
    fi
done <var.txt
echo "You are good to go"

Comments

1

Nice solution from @joe.still ! improvement is to exit after checking all variables

i=0
while read var; do
  [ -z "${!var}" ] && { echo "$var is empty or not set. Exiting.."; let i=i+1; }
done << EOF
KUBE_NAMESPACE
DOCKER_REGISTRY
DOCKER_DEPLOY_USER
DOCKER_DEPLOY_PASSWORD
DOCKER_DEPLOY_EMAIL
EOF

if [ $i -gt 0 ]; then
  echo $i
  echo "exiting"
  exit 1
fi

Comments

0

Good Day Everyone. I've personally used this method in my bash scripts. Verified works on bash 4.4 and later in Ubuntu, openSUSE, and ClearLinux.

Can RHEL|CentOS|Alma and Arch Based users let me know it it works fine for you?

( [ "$VAR1""$VAR2""$VAR3""$VAR4""$VAR5" ] && echo -e " Warning: StackIsNotClear" ) || { echo -e " GoodNews:  StackIsClear"; }

1 Comment

echo -e is unreliable: Depending on how bash is configured, it may print -e as a string on output, and its behavior isn't reliably portable to other POSIX-y shells. When you need its functionality, use printf instead; see the APPLICATION USAGE and RATIONALE sections of pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html, and the extensive answer to the Unix & Linux question Why is printf better than echo?
-1

The function one is nice, but for a simple script I just want as few lines as possible at the start of a script, with a relatively easy to understand error message

required=(DB HOST DATE)
for var in ${required[@]}; do
  [ -z "${!var}" ] && { echo "$var is empty or not set. Exiting.."; exit 1; }
done

1 Comment

Always quote your expansions. "${required[@]}" -- otherwise if, say, IFS=T, you'd have your expansions come out to DB, HOS, DA and E.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.