help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Irregularities when using -v, -z, -n


From: Leonid Isaev (ifax)
Subject: Re: Irregularities when using -v, -z, -n
Date: Thu, 29 Jul 2021 14:21:39 +0000
User-agent: Mutt/1.13.4 (2020-02-15)

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]