Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between unset and empty variables in bash

Tags:

bash

Using bash, what's the best method to check if a variable is empty or not?

If I use:

if [ -z "$VAR" ]

as suggested in a forum this works for an unset variable but it is true when the variable is set but empty. Suggestions?

like image 641
green69 Avatar asked Mar 23 '11 14:03

green69


People also ask

What does unset mean in bash?

Use unset command to delete the variables during program execution. It can remove both functions and shell variables.

How can you determine if a bash variable is unset?

To find out if a bash variable is empty: Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"

What is unset variable in Linux?

Unsetting Variables Once you unset a variable, you cannot access the stored value in the variable. Following is the syntax to unset a defined variable using the unset command − unset variable_name. The above command unsets the value of a defined variable.

Is null or empty bash?

You can quickly test for null or empty variables in a Bash shell script. You need to pass the -z or -n option to the test command or to the if command or use conditional expression.


2 Answers

Unset (non-existant) variables and empty variables behaves differently in parameter expansion:

In the following examples:
Without colon:

Checks only for variable existence.

With colon:

Checks for variable existence, if it exists, make sure that it's not empty.
In other words, checks both for variable existence and non-emptiness.


  • ${parameter:-word}
    If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

  • ${parameter-word}
    If parameter is unset...


  • ${parameter:=word}
    If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.

  • ${parameter=word}
    If parameter is unset...


  • ${parameter:?word}
    If parameter is unset or null, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

  • ${parameter?word}
    If parameter is unset...


  • ${parameter:+word}
    If parameter is unset or null, nothing is substituted, otherwise the expansion of word is substituted.

  • ${parameter+word}
    If parameter is unset...


Source

like image 70
Bohao LI Avatar answered Sep 21 '22 20:09

Bohao LI


${var+set} substitutes nothing if the variable is unset and set if it is set to anything including the empty string. ${var:+set} substitutes set only if the variable is set to a non-empty string. You can use this to test for either case:

if [ "${foo+set}" = set ]; then
    # set, but may be empty
fi

if [ "${foo:+set}" = set ]; then
    # set and nonempty
fi

if [ "${foo-unset}" = unset ]; then
    # foo not set or foo contains the actual string 'unset'
    # to avoid a potential false condition in the latter case,
    # use [ "${foo+set}" != set ] instead
fi

if [ "${foo:-unset}" = unset ]; then
    # foo not set or foo empty or foo contains the actual string 'unset'
fi
like image 42
geekosaur Avatar answered Sep 20 '22 20:09

geekosaur