help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Exclusion of `${##}` from the doc of `${parameter#word}`?


From: Andreas Kusalananda Kähäri
Subject: Re: Exclusion of `${##}` from the doc of `${parameter#word}`?
Date: Sun, 9 May 2021 10:05:10 +0200

${#parameter} is well defined in POSIX for the case when parameter is #.

The following expansions are explicitly unspecified in POSIX for the case
when parameter is # (or @ or *).

        ${parameter#[word]}
        ${parameter##[word]}
        ${parameter%[word]}
        ${parameter%%[word]}

I think it's correct that the bash shell uses the defined POSIX case
before accepting its own extended syntax.  It would be rather awkward to
get the length of $# otherwise.

What is the issue you're trying to resolve in relation to this?


Regards,


On Sat, May 08, 2021 at 11:53:49PM -0500, Peng Yu wrote:
> `${##}` could be interpreted as `${parameter#word}` according to the
> manual, where parameter is `#` and word is empty.
> 
>        ${parameter#word}
>        ${parameter##word}
>               Remove  matching prefix pattern.  The word is expanded
> to produce a pattern just as in path-
>               name expansion, and matched  against  the  expanded
> value  of  parameter  using  the  rules
>               described  under  Pattern Matching below.  If the
> pattern matches the beginning of the value
>               of parameter, then the result of the expansion is the
> expanded value of parameter  with  the
>               shortest matching pattern (the ``#'' case) or the
> longest matching pattern (the ``##'' case)
>               deleted.  If parameter is @ or *, the pattern removal
> operation is  applied  to  each  posi-
>               tional parameter in turn, and the expansion is the
> resultant list.  If parameter is an array
>               variable subscripted with @ or *, the pattern removal
> operation is applied to each member of
>               the array in turn, and the expansion is the resultant list.
> 
> But it actually just counts the number of characters of `$#` for which
> the following rule applies. It seems that the manual is ambiguous on
> this specific case. Is it so?
> 
>        ${#parameter}
>               Parameter  length.   The  length in characters of the
> value of parameter is substituted.  If
>               parameter is * or @, the value substituted is  the
> number  of  positional  parameters.   If
>               parameter  is  an  array  name subscripted by * or @,
> the value substituted is the number of
>               elements in the array.  If parameter is an indexed array
> name subscripted by a negative num-
>               ber, that number is interpreted as relative to one
> greater than the maximum index of parame-
>               ter, so negative indices count back from the end of the
> array, and an index of -1 references
>               the last element.
> 
> $ set -- {a..z}; echo ${##}
> 2
> 
> -- 
> Regards,
> Peng

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]