0

I'm in the unfortunate position to be forced to invoke a program via echo <input> | program.exe. Of course, I wondered how to escape <input> and found:

In essence, it seems sufficient to escape all special chars with ^. Out of curiosity I still would like to know, why echo ingores double-quote escaping in the first place:

C:\>echo "foo"
"foo"

C:\>

Is there any normative reference?

Bonus question: How to echo the strings on and off with the echo command?

Edit: I just found this. I states that only |<> need to be escaped. However, expansion like %FOO% still work.

code_onkel
  • 2,759
  • 1
  • 16
  • 31
  • 2
    for the Bonus: `echo/on` and `echo/off` (note: no space after `echo`) (same method is used to `echo` a possibly empty variable without getting "ECHO is on": `echo/%emty%`. There are some charactes, you can use instead of `/` (best is `(`, but it can be confusing to read). There is a very good explanation about that somewhere. I'll try to find it again... – Stephan Jun 02 '17 at 08:16
  • This is funny indeed. The doc I found states `.` as the character to be used. `,;:` work as well. – code_onkel Jun 02 '17 at 08:28
  • 2
    [found it](http://www.dostips.com/forum/viewtopic.php?f=3&t=774&hilit=echo+blank+line) In short: `(` seems odd but is the most reliable one. – Stephan Jun 02 '17 at 08:42
  • Quote marks appear fairly frequently in English. It would be inconvenient if `echo` treated them as special characters. – Harry Johnston Jun 02 '17 at 08:48
  • 1
    it's not only `|<>`, but also `&^` and in some cases even `)%` – Stephan Jun 02 '17 at 08:48
  • @HarryJohnston That is plausible, of course. However, it ramifies the shell syntax by applying different escaping rules for special built-in commands. – code_onkel Jun 02 '17 at 08:57
  • 1
    Not exactly; shell characters that are inside quote marks are always ignored, and that's all the shell itself does. It is up to each individual command (regardless of whether it is an internal or external command) to decide what, if anything, the quote marks mean. – Harry Johnston Jun 02 '17 at 10:51
  • ECHO isn't the only internal command that treats quotes as normal characters; SET, for example, includes them in values. That is, `set var="value"` would store the quotes as part of the value of the variable. Also, this SO answer is related to this question: https://stackoverflow.com/a/37911799/2712565 – GlennFromIowa Jul 10 '18 at 23:39

1 Answers1

1

Special characters like ^, &, (, ), <, >, %, ! and " may cause problems with echo, also when trying to echo a string into a pipe; odd numbers of " are particularly difficult to handle.

Building escape sequences can be very complicated particularly with pipes, because such initiates new cmd instances for either side, so multi-escaping might become necessary.

The only reliable way to pipe the output of echo into a program is to use a variable holding the string to return and to apply delayed expansion, but within the left side of the pipe, like this:

cmd /V /C echo(^^!VARIABLE^^!| program.exe

Note the double-escaping of ! like ^^!, which makes this code even work when delayed expansion is also enabled in the parent cmd instance. There must not be a SPACE in front of the |, because this was echoed too otherwise. Note that echo terminates the output by a line-break.

aschipfl
  • 33,626
  • 12
  • 54
  • 99