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

Re: Counting characters in command output?



> I myself almost always want lines.  Or elements.  Sometimes words, almost  never characters.

As I said, it's the consistency. 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.


On Wed, Feb 14, 2024 at 10:37 AM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:


On 2024-02-13 22:49, Roman Perepelitsa wrote:
Right, command substitution. In my opinion, it's unfortunate that
command substitution in zsh splits on IFS by default. I wish this
wasn't the case. It's not even common that one wants to split command
output into words. Lines -- perhaps, but not words. So the default
behavior in this case is rarely what is desired and has to be actively
turned off.
What are the vectors?  First must be logical necessity, second consistency -- avoid strange exceptions -- third and fourth would be helpfulness and tradition, with old-school guys favoring tradition and guys like me favoring helpfulness.  I've spent more hours trying to get splitting issues correct than anything else.  Characters, words, IFS, lines, elements ... it's the  most obscure and confusing part of the shell.  Lacking any focused explanation of the entire subject what I do is just throw saved snippets of code at problems until something sticks -- it looks about right, but even there I might think I have line splitting when it's really elements.  I wish there were enough of us on this list to put things to a vote sometimes and in this case I'd bet that Roman's view would win overwhelmingly.   I myself almost always want lines.  Or elements.  Sometimes words, almost  never characters.





--
Mark J. Reed <markjreed@xxxxxxxxx>


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