Bash Shell: Find Out If a Variable Is Set or Not

How do I check if a bash shell variable called $input is defined or not under BSD / Apple OS X / Unix / Linux like operating systems?

Method #1: Bash Variable Existence Check

The syntax is as follows to determine if $input is defined or not:

${Variable?Error $Variable is not defined}


${Variable:?Error $Variable is not defined}

In this example your script will stop executing if the variable $input is not defined:

input="Foo bar"
echo ${input?Error $input is not defined.}
unset input
echo ${input?Error $input is not defined.}

Sample outputs:

Foo bar
bash: input: Error $input is not defined.

In this example, make sure $input is defined and is not empty, enter:

[[  $input && ${input-x} ]] 
[[  $input && ${input-x} ]] && echo "Found" || echo "Not found"
unset input
[[  $input && ${input-x} ]] && echo "Found" || echo "Not found"

Here is an example that make sure $_php_map_extension is defined:

        # read config data
        loadConfigData "${_t_domain_php_conf}" $LINENO "${FUNCNAME[0]}"
	# make sure it is defined and not empty 
	if [[ $_php_map_extension && ${_php_map_extension-_} ]]
                at=${#_php_map_extension[*]}                                            # get total elements in an array
                echo '## Map extension to .php? ##' 
                echo ' = ('  
                for (( i=0; i<${at}; i++ ));
                        [ $i -lt $(( $at - 1 )) ] && s="," || s=""                      # remove , for last item in an array                    
                        echo "      ".${_php_map_extension[i]}" => ".php"${s}" 
                echo ')'
		echo "Skiping php map extension as $_php_map_extension is not defined in /usr/local/etc/nixcraft/conf/php.conf."

Sample outputs:

Skiping php map extension as $_php_map_extension is not defined in /usr/local/etc/nixcraft/conf/php.conf


## Map extension to .php? ## = (
      ".html" => ".php",
      ".htm" => ".php",
      ".phtml" => ".php",
      ".php3" => ".php",
      ".php4" => ".php"

Method #2: isvarset() function

The above examples are useful for a sanity checking. Finally, you can use the following code:

 local v="$1"
 [[ ! ${!v} && ${!v-unset} ]] && echo "Variable not found." || echo "Variable found."
# find out if $vech defined or not
vech="Bus" && isvarset vech
vech="" && isvarset vech
unset vech && isvarset vech

Method 3: Using the Length of STRING

The -z option to test command returns TRUE of the Length of STRING is zero. You can use the following syntax:

### set or not???
[ -z "${input+x}" ] && echo "$input is not set" || echo "$input found and is set to "$input"."
### Not set at ALL
unset input
[ -z "${input+x}" ] && echo "$input is not set" || echo "$input found and is set to "$input"."
### 'set but empty' or not?
[ -z "$input" -a "${input+x}" = "x" ] && echo "$input variable is set with empty value." || echo "$input found and is set to "$input""

The above syntax will tell if a variable is defined or not defined or defined with a empty value in a bash shell script.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts