bug-bash
[Top][All Lists]
Advanced

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

Re: bash tries to parse comsub in quoted PE pattern


From: Greg Wooledge
Subject: Re: bash tries to parse comsub in quoted PE pattern
Date: Wed, 18 Oct 2023 10:11:12 -0400

On Wed, Oct 18, 2023 at 09:39:36AM -0400, Zachary Santer wrote:
> I guess I still want to hear about "${#@}" and $[  ].

$[ ] is officially deprecated, and users are advised to stop using it.
It was originally going to be the syntax for arithmetic expansion, and
made it as far as some POSIX rough draft, I think.  But then the
decision was made to go with $(( )) instead.

Meanwhile, bash had already shipped releases with support for $[ ] and
people had already started using it.  So, it's still *there*, and still
works, despite being deprecated for literally decades.  I don't know
whether it'll ever actually be removed -- maybe it'll just linger until
POSIX decides to use $[ ] for something else and forces a change.

${#@} is simply the use of @ as a pseudo array name, which works in many
places, the most common being "$@".  For example,

unicorn:~$ set -- one two three four
unicorn:~$ echo "${@//o/x}"
xne twx three fxur

In this context, @ can be thought of as a shortcut for argv[@] where
argv contains the positional parameters.  ${#@} is therefore analogous
to ${#argv[@]} which counts the number of elements in an array named
argv.

It's not a perfect analogy, though.  For example, this doesn't work:

unicorn:~$ echo "${!@}"
bash: one two three four: invalid variable name

That's due to the syntax collision between ${!ref} and ${!array[@]}.
The parser tries to apply the former rather than the latter.

In practical terms, there is no reason to use ${#@}.  It's the same
as $# except that the latter is shorter and more portable.  But at the
same time, there's no reason to say anything about ${#@}, since nobody
ought to be using it anyway.  And if they do use it, what's the harm?



reply via email to

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