[Toybox] Would someone please explain what bash is doing here?

Chet Ramey chet.ramey at case.edu
Wed Apr 1 15:34:09 PDT 2020


On 3/30/20 6:59 PM, Rob Landley wrote:

> Aha:
> 
>   $ readonly SECONDS; SECONDS=0; echo $SECONDS; echo hello
>   $ readonly SECONDS; SECONDS=0
>   $ echo $?
>   1
> 
> No error message. Line processing aborted despite ; not being &&.

The assignment function for SECONDS ignored it, but the higher-level code
noted an assignment to a readonly variable and treated it as an assignment
error. Assignment errors abort the processing of the current command,
which in this case is the list.

>   $ POTATO=123 echo hello
>   bash: POTATO: readonly variable
>   hello
> 
> Ok, "consistent" is the wrong word.

Assignment errors preceding a simple command cause the command to return
an error.

> 
>   $ bash -c $'SECONDS=42; readonly SECONDS; SECONDS=0\necho $SECONDS'
>   0
> 
> And the assignment IS still zeroing it, despite the readonly and error.

Yep. Dynamic variables.

>   $ bash -c 'SECONDS=42; echo $SECONDS; SECONDS=123+456; echo $?; echo $SECONDS'
>   42
>   0
>   0
> 
> And the "ignores integer, goes to zero instead" thing is not an error either.

I do think SECONDS and RANDOM, at least, should accept arithmetic
expressions on assignment. I'll make that change.

Chet


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet at case.edu    http://tiswww.cwru.edu/~chet/



More information about the Toybox mailing list