[Toybox] bash continues to confuse me.

Chet Ramey chet.ramey at case.edu
Mon Jul 6 12:20:10 PDT 2020


On 7/4/20 5:29 AM, Rob Landley wrote:

> Except arrays index from _1_ not from _0_, and the FUN part is:
> 
>   $ xx() { echo ${*:0};}; xx a b c
>   bash a b c

Arrays don't index from 1. Positional parameters index from 1.

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

$0 is not one of the positional parameters; it's a special parameter all
its own. The positional parameters don't include $0.

> 
> 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):

That's correct and what Bourne shells do. This is why I introduced
FUNCNAME.

> 
>   $ 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),

That's what happens with -c, yes. Again, every shell does this. If you
don't use -c, like if you're running a shell script, the arguments to the
script become the positional parameters.


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

The standard doesn't provide it to you at all, and most shells don't have a
way to obtain it.

-- 
``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