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 15:38:06 +0200

Have begun to understand a little bit from your comments.

  local aa

  [[ -v aa ]] && echo "[[ -v aa ]]: Variable exists."
  [[ ! -v aa ]] && echo "[[ ! -v aa ]]: Variable unknown."

  [[ -z "$aa" ]] && echo "[[ -z \"\$aa\" ]]: Variable exists, size is zero."
  [[ ! -z "$aa" ]] && echo "[[ ! -z \"\$aa\" ]]: Variable exists, size is 
positive (not-zero)."

  [[ -n "$aa" ]] && echo "[[ -n \"\$aa\" ]]: Variable exists, size is positive 
(not-zero)."
  [[ ! -n "$aa" ]] && echo "[[ ! -n \"\$aa\" ]]: Variable exists, size is zero."


Results are

local aa
[[ -z "$aa" ]]: Variable exists, size is zero.
[[ ! -n "$aa" ]]: Variable exists, size is zero.

This means that -z and -n never test existence or if the variable has been set 
or not.
If variable does not exist, it still says that the size is zero (false because 
variable
not actually set to anything).

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







> Sent: Friday, July 30, 2021 at 1: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:04:49PM +0200, eduardo-chibas@caramail.com wrote:
> > How does manual never give examples of how to use things !
> > 6.4 Bash Conditional Expressions
>
> You are welcome to submit documentation improvements...
>
> > Everyone is expected to figure things out by themselves.
>
> In the end, yes.
>
> --
> Leonid Isaev
>
>



reply via email to

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