Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: more splitting
On Wed 15 Apr 2026, at 16:17, Ray Andrews wrote:
> I mean that arrays might have been designed to carry a separator
> character between elements. ASCII FF maybe. So when piped,
> reconstructing an array would be easier.
even if arrays somehow carried a separator like that, print doesn't work
on arrays. it takes a list of string arguments, which it has zero
context for, and prints them as text. that's all
if you wanted to, *you* could have print format the output a certain
way (using `print -N` like philippe was doing), or give it the arguments
pre-formatted (using (q+) like bart was doing). ofc whatever is on the
other end of the pipe would have to understand that format to do
anything useful with it. again there is no standard for structured pipe
data in the unix world, it's all just bytes, and each command has to
decide for itself what bytes to put in and how to interpret the bytes
that come out
On Wed 15 Apr 2026, at 16:17, Ray Andrews wrote:
> % print -n $var
> a b c
> d e f g h ij# # What's the hash for?
adding to what mark said: the option that causes this is prompt_sp. the
output is missing a new-line because of the -n
On Wed 15 Apr 2026, at 16:17, Ray Andrews wrote:
> % var2=( ${(f)var} ); hex var2
>
> 'a b c'
> 'd e f g h ij'
>
> ...
>
> ... love it. Split on newline. All spaces preserved. One thing tho:
> when $var is created, would not the splitting spaces vanish? But we
> see the space between 'b' and 'c' remains in var2. Likewise the space
> between 'h' and 'i' -- how are they retained? Or are they put back for
> display?
they are not retained. this is a quirk of the way splitting flags like
(s) and (f) work. by default they want to treat the parameter to be
split as a scalar. so just like in your earlier example with the
var2=$var assignment, the shell has to convert var into a single element
for it. to do that it joins the elements using the first character of
IFS, which is a space by default. it's equivalent to this:
% var=( 'a b' $'c\nd e f g h' ij )
% tmp_var=$var
% typeset -p tmp_var
typeset tmp_var=$'a b c\nd e f g h ij'
% var2=( ${(f)tmp_var} ); hex var2
'a b c'
'd e f g h ij'
...
since the (f) only splits on \n, you get two elements, one from before
the \n and one from after
if you want it to split each element of var separately, use (@):
% var=( 'a b' $'c\nd e f g h' ij )
% var2=( ${(@f)var} ); hex var2
'a b'
c
'd e f g h'
ij
...
On Wed 15 Apr 2026, at 16:17, Ray Andrews wrote:
> ... so typeset output very close to original variable creation
> keystrokes.
to be clear, typeset is re-quoting everything from scratch because,
again, the shell doesn't store how you quoted things originally. the
quoting style it uses is basically the same as the (q+) style, which is
designed to be minimal (i.e. easier to read). so it will often happen to
match what the way you wrote it, but not necessarily
dana
Messages sorted by:
Reverse Date,
Date,
Thread,
Author