How to check if multiple variables are defined or not in bash

BashShell

Bash Problem Overview


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      

Bash Solutions


Solution 1 - Bash

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:bash] 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.

Solution 2 - Bash

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" 

Solution 3 - Bash

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

Solution 4 - Bash

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"

Solution 5 - Bash

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionramesh.mimitView Question on Stackoverflow
Solution 1 - BashTom FenechView Answer on Stackoverflow
Solution 2 - BashBromateView Answer on Stackoverflow
Solution 3 - BashJoe StillView Answer on Stackoverflow
Solution 4 - BasheQ19View Answer on Stackoverflow
Solution 5 - BashkhancellView Answer on Stackoverflow