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

Re: Apparent inconsistency in f/z expansion flags behavior



On Dec 18,  8:02pm, Pablo Lalloni wrote:
} 
} words=(${(z)$(</proc/meminfo)})
} 
} Which set words with the array of all the words in the file and that's
} great.
} 
} lines=(${(f)$(</proc/meminfo)})
} 
} But then I get an array with just one string containing all the lines
} concatenated (no NLs).

That's happening because $(</proc/meminfo) has replaced all the NLs with
spaces before (z) or (f) begin working.  (z) doesn't care because it
splits on shell-syntax whitespace, but (f) has nothing to split on.

To get what you want, you have to quote the $(...) substitution so the
NLs are preserved:

    lines=(${(f)"$(</proc/meminfo)"})

That would be "more correct" in the (z) use as well, just in case the
line breaks were e.g. inside quoted strings that would change the shell
parse.  (Not an issue with /proc/meminfo, of course, but in general.)

} Note that if you split the last assignment in 2 steps, it works as expected:
} 
} lines=$(</proc/meminfo)
} lines=(${(f)lines})

There you've done first an assignment to a scalar, which preserves the
NLs as if $(...) were quoted.



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