On Thu, Jul 29, 2021 at 03:38:06PM +0200, eduardo-chibas@caramail.com wrote:
> Have begun to understand a little bit from your comments.
First, could you please ditch that crap of a mail client you are using and stop
top-posting? Please use this my reply as an EXACT model of how yours should
look like.
> This means that -z and -n never test existence or if the variable
They test strings, as the manual says.
> If variable does not exist, it still says that the size is zero
Yes, unless "nounset" is enabled:
-----8<-----
help set:
...
nounset same as -u
-u Treat unset variables as an error when substituting.
...
----->8-----
For example:
-----8<-----
I-akula-++-10:16-~-> [[ ! -v x ]] && echo -E "x unset"
x unset
I-akula-++-10:16-~-> [[ -z "$x" ]] && echo -E "x empty"
x empty
I-akula-++-10:16-~-> set -u
I-akula-++-10:16-~-> [[ ! -v x ]] && echo -E "x unset"
x unset
I-akula-++-10:16-~-> [[ -z "$x" ]] && echo -E "x empty"
bash: x: unbound variable
----->8-----
> Thusly, to determine if a variable value is set and not zero size, one has to
> do
>
> if [[ -v var && -n "$var" ]]; then
> echo "Variable is set with finite length."
> fi
There is a logic flaw here: how can a variable be unset but contain a non-empty
string? You don't need [[ -v var ]] -- [[ -n "$var" ]] is sufficient.
--
Leonid Isaev