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

Re: more splitting



On Tue, Apr 14, 2026 at 4:24 PM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>
> What you try to do is pretty much impossible because when you do 'print -rn $var',you turn the strings in '$var' into a single one

Well, you can't do it with just "print" by itself.

> How is 'hex' supposed to know which of the spaces in that string were from the strings in '$var' and which ones were introduced to separate the strings in '$var'?!?

The trick is to restore some kind of quoting on $var.  It's not
possible to restore exactly the original quoting but you can do as
well as "hex" itself does.

If "hex" looks like this ...

function hex ()
{
    local var element
    echo
    if [[ -p /dev/fd/0 ]]; then
       read -d '' -r var
       set -- ${(z)var}
    fi
    print -rC1 -- ${(q+)${(Q)@}}
    echo "\n-----------------------------\n"
    for element ("$@") print -rn -- $element | od -vAx -tx1 -tc
}

Then:

print -rn ${(q+)var} | hex

does the same thing as

hex ${(q+)var}

but you can't avoid that (q+) on the way in to make it sane.

More later about $(<&0) ...




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