[Toybox] bash continues to confuse me.

Rob Landley rob at landley.net
Sat Jul 4 02:29:47 PDT 2020


On 7/3/20 2:15 AM, Rob Landley wrote:
> Ok, with a string, the start being off the left or right edges resolves to empty
> string, but end before start is an error. Length being zero is fine. A window
> including the end of the string gives the right edge of the string, a window
> including the left edge of the string gives nothing?
> 
> With an array, the first argument being negative is the usual "from end", the
> second argument being negative is an ERROR? (What?) And the same arguments off
> left edge produces no output even when the window advances into range with stuff
> in it, but arguments off right edge clip to show you what it can.

Except arrays index from _1_ not from _0_, and the FUN part is:

  $ xx() { echo ${*:0};}; xx a b c
  bash a b c

Which means that ${*} is actually ${*:1} and NOT showing all the available data
that must nevertheless be present in the array.

Except that's also wrong because $0 isn't showing "xx", it's saying "bash". It's
showing the arguments to the function (not the shell) but the name of the shell
(not the function):

  $ bash -c 'echo $0 $*; xx() { echo $0 $*;}; xx ONE TWO THREE' a b c
  a b c
  a ONE TWO THREE

Which is what $0 always does, with the extra weirdness that the first argument
to the shell replaces the name (and if there's only one there are no arguments),
but the first argument to a shell function is an argument and the name of the
function isn't recorded in this at all.... it's $FUNCNAME. (Which is an array
but in this instance that can be ignored.)

Great.

> *shrug* To me, this is a pile of arbitrary behavior.

Easiest way to deal with this is probably special casing 0 in the ${*:0:47}
slice. (And I think my version is handling ${*:-2} with the same "walk back from
end" logic because it would be extra code NOT to for me...)

Rob



More information about the Toybox mailing list