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

Re: zsh adding single quotes as it pleases



"Jerry Rocteur" wrote:
> Given a text file like this:
> 
> cat ll.txt
> -a p1vkg/Bis1234/"XXXXXXXX XXXXXXX XX"
> -a r0esmm/Bis1234/"XXX XXX - XXXX"
> 
> Processing it like this:
> 
> #!/usr/bin/ksh
> 
> cat ll.txt | sed -e '/^\ *\#/d' | while read line
> do
> 
>     set -x
> 
>     echo $line
> 
> done
> 
> 
> Gives me
> 
> [113] + echo -a 'p1vkg/Bis1234/"XXXXXXXX' XXXXXXX 'XX"'
> -a p1vkg/Bis1234/"XXXXXXXX XXXXXXX XX"
> [113] + read line
> [113] + set -x
> [113] + echo -a 'r0esmm/Bis1234/"XXX' XXX - 'XXXX"'
> -a r0esmm/Bis1234/"XXX XXX - XXXX"
> [113] + read line
> 
> zsh is adding single quotes..

You mean the quotes in the set -x output?  The real question is why you
expect or need that to be different.

> Why does it do that ?

$line contains:

-a p1vkg/Bis1234/"XXXXXXXX XXXXXXX XX"

In ksh mode, this gets split into words on spaces when used as a command
argument, i.e the words:

-a
p1vkg/Bis1234/"XXXXXXXX
XXXXXXX
XX"

The set -x output sees those words.  It notices that some contain a
special character, '"'.  An echo statement with those in needs
quoting; the most convenient way of quoting the word is with single
quotes.  So zsh comes up with:

echo -a 'p1vkg/Bis1234/"XXXXXXXX' XXXXXXX 'XX"'

As you'll see, this statement exactly produces the output you get
from the original "echo $line".  Without the single quotes, it wouldn't.

> How can I stop it doing this ?

You can't, because then the output would be wrong.  What you are
actually trying to do?  There are various other ways of reading the
variable so it doesn't get split, for example, or displaying the output,
or...?

-- 
Peter Stephenson <pws@xxxxxxx>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom



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