I would like to know if there is a difference in how bash represents variables with null values
var=
and with empty strings
var=""
What precautions should I take when handling variables like these?
I would like to know if there is a difference in how bash represents variables with null values
var=
and with empty strings
var=""
What precautions should I take when handling variables like these?
Consider the following:
var1=
var2=""
The variable var1
has no value, has its value null ( null
). A null value is exactly a "NOTHING"! Different from the value of the var2
variable that has a blank string.
Imagine that we will be using some object language (just to stay more didactic). Using the example with Python:
$ python
Python 2.7.12 (default, Jun 29 2016, 14:05:02)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> var1 = None
>>> var2 = ""
>>> type(var1)
<type 'NoneType'>
>>> type(var2)
<type 'str'>
ps: None
in python is the same as null
in other languages.
The var1
situation demonstrates that there is no initialization of a value. So, a "nothing" can not receive a return, if you try ... you will get the famous error NullPointerException
because nothingness can not receive attribute (s): it does not receive values and points to nothing.
>>> len(var1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'NoneType' has no len()
% w /% is a variable of type var2
empty. Something "empty" has its value of zero because this way the variable is initialized. That is, even being a "blank character" it has its place reserved in memory. Then, when using string
(length) will be returned len()
:
>>> len(var2)
0