Based on this, amongst many other things I've read, my understanding is that a='' makes a both null and zero-length. But, then, how does one create a zero-length, non-null string? Or, is there no such thing? To the extent this is shell-dependent, I'm working in bash.
- 67,283
- 35
- 116
- 255
- 370
- 3
- 11
1 Answers
"A null string" means "a zero length (empty) string". See e.g. the POSIX definition of "null string". This means that there is no such thing as a non-null zero length string.
However, there is such a thing as an unset variable.
var=''
unset uvar
There's now a difference between var and uvar after running the above code. For example, ${var-hello} would expand to the empty string since var is set, while ${uvar-hello} would expand to hello since uvar is unset. Likewise, ${var+hello} would expand to hello since var is set, and ${uvar+hello} would expand to an empty string since uvar is unset (see standard parameter expansions)
In bash, you can also use the -v test to test whether a variable is set or not:
if [ -v variable ]; then
echo variable is set
fi
Again, a variable being "set but empty" is different from a variable being "unset". A string (contents of a variable) can't be null and at the same have non-zero length.
In other languages, an array of characters may contain nul bytes (\0), which means that you may have an array starting off with a nul byte, and then containing some text after that (terminated with another nul byte). When that is interpreted as a string, that string would have zero length, but the contents of the array would not be empty.
Most shells (apart from zsh) does not allow nul bytes in variables though.
- 333,661
-
You switch from "null" to "nul", are they different? Most shells don't allow nul(l) bytes anywhere in variables, or just at the start? – TTT Jun 12 '19 at 17:55
-
1@TTT "nul byte" means a
\0character. "Null" means "empty". They are distinct. Most shells don't allow nul bytes anywhere in variables (as it is what terminates the internal representation of a string). – Kusalananda Jun 12 '19 at 17:57
a null string in that context is a string of length 0 containing no byte at all– Jeff Schaller Jun 12 '19 at 17:36