[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Arithmetic evaluation / expansion question
From: |
Andy Chu |
Subject: |
Re: [Help-bash] Arithmetic evaluation / expansion question |
Date: |
Wed, 10 Oct 2018 10:31:39 -0700 |
To be clearer here is an example of the recursive eval (in $(( )) rather
than [[ ]], but it's the same thing in both places):
$ one=1
$ two=2
$ x='one + two'
$ code='4 > 2 ? x : y'
$ echo $(( code ))
3
But don't do that :) Or use eval explicitly.
On Wed, Oct 10, 2018 at 10:25 AM Andy Chu <address@hidden> wrote:
> Right, I have seen this very odd recursive behavior. It doesn't appear to
> be documented, but one of the very first bugs filed against OSH mentions it:
>
> https://github.com/oilshell/oil/issues/3
>
> In this case it's one the operands of [[ a -eq b ]]. Not only can 'a' be
> a variable that contains a varaible name, etc. -- it can be an entire
> arithmetic expression.
>
> In other words, it can be thought of as an implicit recursive 'eval' as
> well. Basically bash tries as hard as it can to make things integers in
> arithmetic contexts. It's even more aggressive than awk or Make, which
> also work with integers-as-strings.
>
> (I chose not to copy this behavior since "important" shell scripts don't
> appear to rely on it.)
>
> Andy
>
>
>
> On Wed, Oct 10, 2018 at 10:09 AM Greg Wooledge <address@hidden>
> wrote:
>
>> On Wed, Oct 10, 2018 at 12:47:05PM -0400, Bruce Hohl wrote:
>> > Are strings also evaluated to zero within $(( ))?
>> >
>> > $ pd=abc
>> > $ echo $pd
>> > abc
>> > $ echo $((pd))
>> > 0
>>
>> As Andy said, things that can be parsed as variable names are treated
>> as variable names in arithmetic contexts. What he didn't mention was
>> that bash does this *recursively* until it gets to an integer, or to
>> something that can't be parsed as either an integer or a variable name.
>>
>> wooledg:~$ a=b b=c c=d d=e e=f f=42; echo $((a))
>> 42
>>
>>
- [Help-bash] Arithmetic evaluation / expansion question, Bruce Hohl, 2018/10/10
- Re: [Help-bash] Arithmetic evaluation / expansion question, Andy Chu, 2018/10/10
- Re: [Help-bash] Arithmetic evaluation / expansion question, Greg Wooledge, 2018/10/10
- Re: [Help-bash] Arithmetic evaluation / expansion question, Andy Chu, 2018/10/10
- Re: [Help-bash] Arithmetic evaluation / expansion question,
Andy Chu <=
- Re: [Help-bash] Arithmetic evaluation / expansion question, Bruce Hohl, 2018/10/10
- Re: [Help-bash] Arithmetic evaluation / expansion question, Andy Chu, 2018/10/10
- Re: [Help-bash] Arithmetic evaluation / expansion question, Bruce Hohl, 2018/10/10
- Re: [Help-bash] Arithmetic evaluation / expansion question, Quentin L'Hours, 2018/10/10
- Re: [Help-bash] Arithmetic evaluation / expansion question, Bruce Hohl, 2018/10/10
- Re: [Help-bash] Arithmetic evaluation / expansion question, Chet Ramey, 2018/10/11
- Re: [Help-bash] Arithmetic evaluation / expansion question, Chet Ramey, 2018/10/11
Re: [Help-bash] Arithmetic evaluation / expansion question, Chet Ramey, 2018/10/11
Re: [Help-bash] Arithmetic evaluation / expansion question, Chet Ramey, 2018/10/11