zsh-workers
 help / color / mirror / code / Atom feed
* there should be a way to echo with quotes or escaping
@ 2005-03-23 22:00 Dave Yost
  2005-03-24  1:25 ` Philippe Troin
  2005-03-24  1:34 ` Bart Schaefer
  0 siblings, 2 replies; 5+ messages in thread
From: Dave Yost @ 2005-03-23 22:00 UTC (permalink / raw)
  To: zsh-workers


Z% echo "abc def"
abc def
Z% echoquoted "abc def"
'abc def'
Z% echoescaped "abc def"
abc\ def
Z%

or some such

If there is such a feature, I didn't see it mentioned under the echo 
command in the man page.

Thanks

Dave


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: there should be a way to echo with quotes or escaping
  2005-03-23 22:00 there should be a way to echo with quotes or escaping Dave Yost
@ 2005-03-24  1:25 ` Philippe Troin
  2005-03-24  2:03   ` Dave Yost
  2005-03-24  1:34 ` Bart Schaefer
  1 sibling, 1 reply; 5+ messages in thread
From: Philippe Troin @ 2005-03-24  1:25 UTC (permalink / raw)
  To: Dave Yost; +Cc: zsh-workers

Dave Yost <Dave@Yost.com> writes:

> Z% echo "abc def"
> abc def
> Z% echoquoted "abc def"
> 'abc def'
> Z% echoescaped "abc def"
> abc\ def
> Z%
> 
> or some such
> 
> If there is such a feature, I didn't see it mentioned under the echo
> command in the man page.

% a="foo' bar"
% echo ${(q)a}
foo\'\ bar
% echo ${(qq)a}
'foo'' bar'
% echo ${(qqq)a}
"foo' bar"

Note that I have rc_quotes set, otherwise:

% echo ${(qq)a}
'foo'\'' bar'

Phil.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: there should be a way to echo with quotes or escaping
  2005-03-23 22:00 there should be a way to echo with quotes or escaping Dave Yost
  2005-03-24  1:25 ` Philippe Troin
@ 2005-03-24  1:34 ` Bart Schaefer
  1 sibling, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2005-03-24  1:34 UTC (permalink / raw)
  To: zsh-workers

On Mar 23,  2:00pm, Dave Yost wrote:
} 
} If there is such a feature, I didn't see it mentioned under the echo 
} command in the man page.

The way to do this is with parameter expansion:

Z% raw="abc def"
Z% echo ${(q)raw}
abc\ def
Z% echo ${(qq)raw}
'abc def'
Z% echo ${(qqq)raw}
"abc def"
Z% echo ${(qqqq)raw}
$'abc def'

(More than four "q"s doesn't change anything after that.)

To skip the assignment to "raw", just:

Z% echo ${(q):-"abc def"}
abc\ def

Of course you're really better off using "print -R" than "echo", because
"echo" may re-interpret backslashes etc. embedded in its arguments.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: there should be a way to echo with quotes or escaping
  2005-03-24  1:25 ` Philippe Troin
@ 2005-03-24  2:03   ` Dave Yost
  2005-03-24  2:55     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Dave Yost @ 2005-03-24  2:03 UTC (permalink / raw)
  To: zsh-workers

At 5:25 PM -0800 2005-03-23, Philippe Troin wrote:
>Dave Yost <Dave@Yost.com> writes:
>
>> Z% echo "abc def"
>> abc def
>> Z% echoquoted "abc def"
>> 'abc def'
>> Z% echoescaped "abc def"
>> abc\ def
>> Z%
>>
>> or some such
>>
> > If there is such a feature, I didn't see it mentioned under the echo
>> command in the man page.
>
>% a="foo' bar"
>% echo ${(q)a}
>foo\'\ bar
>% echo ${(qq)a}
>'foo'' bar'
>% echo ${(qqq)a}
>"foo' bar"
>
>Note that I have rc_quotes set, otherwise:
>
>% echo ${(qq)a}
>'foo'\'' bar'
>
>Phil.

This should be documented.  If it's in the man pages somewhere, it's not in a form that one can grep for.  These searches in zshall come up with nothing:
  \${\(q
  qqq
  rc_quotes
and there should be some mention under the echo command.

Furthermore, none of these three choices is best.  Forgetting the rc_quotes option, which is for aliens, they all resort to backslash for shell metacharacters like $ ` etc.

229 Z% x="a 'b' "'$a `date` "c"'
233 Z%   setopt rc_quotes ; echo ${(q)x} ; echo ${(qq)x} ; echo ${(qqq)x}  
a\ \'b\'\ \$a\ \`date\`\ \"c\"
'a ''b'' $a `date` "c"'
"a 'b' \$a \`date\` \"c\""
234 Z% unsetopt rc_quotes ; echo ${(q)x} ; echo ${(qq)x} ; echo ${(qqq)x}  
a\ \'b\'\ \$a\ \`date\`\ \"c\"
'a '\''b'\'' $a `date` "c"'
"a 'b' \$a \`date\` \"c\""

Perhaps there should be an option (qqqq)? that does something more like what a human would do, like this:

"a 'b' "'$a `date` "c"'

Thanks

Dave


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: there should be a way to echo with quotes or escaping
  2005-03-24  2:03   ` Dave Yost
@ 2005-03-24  2:55     ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2005-03-24  2:55 UTC (permalink / raw)
  To: zsh-workers

On Mar 23,  6:03pm, Dave Yost wrote:
} Subject: Re: there should be a way to echo with quotes or escaping
}
} This should be documented.

It is.  It's under "parameter expansion", along with all the other
expansion flags.

} and there should be some mention under the echo command.

Why?  It has nothing to do with the echo command, or with any other
particular command for that matter.  You can use parameter expansion
anywhere.

} These searches in zshall come up with nothing:
}   \${\(q
}   qqq

The first of those depends on finding a literal example, and the second
assumes that "qqq" would be documented separately from "q" by itself,
which it happens not to be.  Did you happen to think of searching for
the word "quote" all by istelf?

man zshall | grep -i quote

produces 159 lines of output among which is

       q      Quote the resulting words with  backslashes.  If  this  flag  is
              given twice, the resulting words are quoted in single quotes and
              if it is given three times,  the  words  are  quoted  in  double
              quotes.  If it is given four times, the words are quoted in sin-
              gle quotes preceded by a $.

It mentions "quote" so many times, grep extracts the entire paragraph!

}   rc_quotes

This really has nothing to do with it, either, but if you had used
"grep -i" you'd have found it.  (Well, actually, you might not, because
it's rendered in boldface which "man" emits as backspace-overstriking,
so you have to put "man" through something that undoes the overstrike
before you grep it.  If you want a searchable format, don't use "man".)

} Furthermore, none of these three choices is best.  Forgetting the
} rc_quotes option, which is for aliens, they all resort to backslash
} for shell metacharacters like $ ` etc.

"Best" by what definition?

The point is that you specify what outermost quotes you want used, so
that you know what to expect when you embed the expansion another
context.  The problem with "quote like a human" is that zsh can't know
what the surrounding context will be when the expansion finishes, so
it doesn't know how to begin or end.

I wrote some code for zmail, years ago, that attempted to do "quote
like a human" and the border cases (where the string to be quoted itself
ends in a quote, for example) are a disaster.  Most of the time it works
like you seem to think would be "best" but occasionally you get insane
runs of '"'"'" that it's impossible to decipher.  The occasional back-
slash you get with ${(qq)...} really is much preferable.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-03-24  2:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-23 22:00 there should be a way to echo with quotes or escaping Dave Yost
2005-03-24  1:25 ` Philippe Troin
2005-03-24  2:03   ` Dave Yost
2005-03-24  2:55     ` Bart Schaefer
2005-03-24  1:34 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).