Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Counting characters in command output?





On 2024-02-14 07:58, Mark J. Reed wrote:
> I myself almost always want lines.  Or elements.  Sometimes words, almost  never characters.

As I said, it's the consistency.
Right.  That would be second on my list of vectors and should trump tradition, especially when, as Roman says, most users don't prefer the status quo.

If you have a scalar parameter named foo, $#foo is the number of characters. If it's an array, $#foo is the number of elements.  The key point in my mind is that when you assign a parameter from command substitution, e.g. foo=$(bar), then what you get is a scalar parameter, not an array. You can get an array instead, but you have to ask for it explicitly by putting extra parentheses around the right hand side: foo=($(bar)). So parameter assignment defaults to scalar mode and requires extra punctuation to do array mode. But directly counting the result of the substitution with no intervening parameter defaults to array mode and requires extra punctuation to do scalar mode.
So really there is a logical problem too.  We have another 'invisible' transformation.  We 'have' quarts but liters are what's counted, yes? 

> I would in general have expected foo $(bar)  to behave identically to baz=$(bar); foo $baz.

Yes, it would be non-negotiable in algebra.  a=b; b=c; ergo a=c.



Messages sorted by: Reverse Date, Date, Thread, Author