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

Rob Landley rob at landley.net
Mon Mar 30 15:59:06 PDT 2020


On 3/30/20 5:45 PM, Rob Landley wrote:
> Bash's handling of $SECONDS is kind of inconsistent:
> 
> $ SECONDS=0; echo $SECONDS # why does this print nothing?
> $ readonly SECONDS; declare -p SECONDS ; SECONDS=0
> declare -ir SECONDS="6"
> $ echo $SECONDS # readonly was ignored, assignment happened anyway
> 10
> $ SECONDS=123+456 # it STARTS integer, but integer assignment sets it to 0
> $ echo $SECONDS
> 6
> $ declare -i SS
> $ SS=123+456
> $ echo $SS  # integer assignment works elsewhere just fine?
> 579
> 
> I can't be the first person to test these corner cases, can I? (Version 4.4.12
> as in devuan ascii.)
> 
> The downside is I'm trying to get a test suite that both bash and toysh passes,
> and to do that I'd have to reproduce it ignoring -i and ignoring -r and I kinda
> dowanna?

Aha:

  $ readonly SECONDS; SECONDS=0; echo $SECONDS; echo hello
  $ readonly SECONDS; SECONDS=0
  $ echo $?
  1

No error message. Line processing aborted despite ; not being &&.

  $ POTATO=abc; readonly POTATO; POTATO=42; echo $POTATO
  bash: POTATO: readonly variable

Well, the line aborting despite ; not being && is at least _consistent_, but...

  $ POTATO=123 echo hello
  bash: POTATO: readonly variable
  hello

Ok, "consistent" is the wrong word.

  $ bash -c $'SECONDS=42; readonly SECONDS; SECONDS=0\necho $SECONDS'
  0

And the assignment IS still zeroing it, despite the readonly and error.

  $ 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.

Sigh. At least all this nonsense is reproducible. I can add it to the test suite
and make mine do what bash does, I just... really don't want to?

Rob


More information about the Toybox mailing list