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.
- 66,199
- 35
- 114
- 250
- 360
- 3
- 11
-
2if it's zero-length, how could it be non-null? – Jeff Schaller Jun 12 '19 at 17:21
-
@JeffSchaller It sounds like your answer to the second half of my question, "Or, is there no such thing?", is "yes". – TTT Jun 12 '19 at 17:26
-
I just wasn't sure if you intended something else, like "set" vs "unset", because it sounded like a contradiction to me. – Jeff Schaller Jun 12 '19 at 17:33
-
It seems to me that the answers to the question you linked to would answer this one; do you agree, or what part is left over? e.g. `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
-
Frankly, just being able to be certain whether, in the context of a set variable, "zero-length", "empty", and "null" are the same thing is what I've been struggling with. – TTT Jun 12 '19 at 17:50
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.
- 320,670
- 36
- 633
- 936
-
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 `\0` character. "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