Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using unset vs. setting a variable to empty

I'm currently writing a bash testing framework, where in a test function, both standard bash tests ([[) as well as predefined matchers can be used. Matchers are wrappers to '[[' and besides returning a return code, set some meaningful message saying what was expected.

Example:

string_equals() {     if [[ ! $1 = $2 ]]; then             error_message="Expected '$1' to be '$2'."              return 1     fi } 

So, when a matcher is used, and it fails, only then an error_message is set.

Now, at some point later, I test whether the tests succeeded. If it succeeded, I print the expectation in green, if it failed in red.

Furthermore, there may be an error_message set, so I test if a message exists, print it, and then unset it (because the following test may not set an error_message):

if [[ $error_message ]]; then     printf '%s\n' "$error_message"      unset -v error_message fi 

Now my question is, if it is better to unset the variable, or to just set it to '', like

error_message='' 

Which one is better? Does it actually make a difference? Or maybe should I have an additional flag indicating that the message was set?

like image 217
helpermethod Avatar asked Sep 04 '12 11:09

helpermethod


People also ask

How do you empty a variable in bash?

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"

How do I assign a null value to a variable in bash?

Use the Bash null command to assign a variable if not already set. With the shell parameters expansion, you can assign a default value to a variable if that variable wasn't set. For example, ${myVar:=myDefaultValue} would set the variable myVar to the default value myDefaultValue .

How do you unset a variable?

The local variable created will be available for current session. To unset the variable simply set the value of variable to '' .


2 Answers

Mostly you don't see a difference, unless you are using set -u:

/home/user1> var="" /home/user1> echo $var  /home/user1> set -u /home/user1> echo $var  /home/user1> unset var /home/user1> echo $var -bash: var: unbound variable 

So really, it depends on how you are going to test the variable.

I will add that my preferred way of testing if it is set is:

[[ -n $var ]]  # True if the length of $var is non-zero 

or

[[ -z $var ]]  # True if zero length 
like image 154
cdarke Avatar answered Oct 20 '22 17:10

cdarke


As has been said, using unset is different with arrays as well

$ foo=(4 5 6)  $ foo[2]=  $ echo ${#foo[*]} 3  $ unset foo[2]  $ echo ${#foo[*]} 2 
like image 26
Zombo Avatar answered Oct 20 '22 16:10

Zombo