help-bash
[Top][All Lists]
Advanced

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

Irregularities when using -v, -z, -n


From: eduardo-chibas
Subject: Irregularities when using -v, -z, -n
Date: Thu, 29 Jul 2021 16:45:57 +0200





> Sent: Friday, July 30, 2021 at 2:35 AM
> From: eduardo-chibas@caramail.com
> To: leonid.isaev@ifax.com
> Cc: help-bash@gnu.org
> Subject: Irregularities when using -v, -z, -n
>
> > Sent: Friday, July 30, 2021 at 2:21 AM
> > From: "Leonid Isaev (ifax)" <leonid.isaev@ifax.com>
> > To: help-bash@gnu.org
> > Subject: Re: Irregularities when using -v, -z, -n
> >
> > 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.
>
> Then suggest a mail client to use rather than bitching.
>
> > > 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.

You are right.  But to see if variable is set and zero length, then the -v  
would be needed.
Or perhaps there is same other logic flaw.

> > --
> > Leonid Isaev
> >
> >
>
>



reply via email to

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