bug-bash
[Top][All Lists]
Advanced

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

Re: Error message garbage when parameter expansion used inside (()) and


From: Daniel Mills
Subject: Re: Error message garbage when parameter expansion used inside (()) and variable unset
Date: Tue, 3 Apr 2018 08:56:35 -0400

On Mon, Apr 2, 2018 at 5:16 PM, PRussell <address@hidden>
wrote:

>
> echo 4B
>   ( set -x;var=5;var1=var; (( var1 == $var2 )) && echo yes || echo no )
>
>
> It appears that 3A and 4A evaluate to 0 because of the arithmetic context.
> 3A echo's yes; 4A echo's no.
>
> The problem is what is happening with 3B and 4B.  I tested on bash 4.3.11
> and
> bash 4.4.19 and got a slightly different error message.
>
> Bash version 4.3.11:
>
> ./tt: line 18: var1: var1 ==  : syntax error: operand expected (error
> token is "==  ")
>
> Bash version 4.4.19 (???? was garabage):
>
> ./tt: line 18: ????: var1 ==  : syntax error: operand expected (error
> token is "==  ")
>
> Peggy Russell
>
>
The problem here is quite simple. When using "$", the parameter is expanded
first. Since
it's empty, it expands to the empty string, and the (( keyword simply sees
(( var1 == )),
which is absolutely a syntax error. When not using "$", no expansions
happen before the
(( keyword sees the variable name, and so it uses the name correctly. So
the fix here is
simple: provide variable names without using "$" in an arithmetic context.


reply via email to

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