Reputation: 2947
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.
Upvotes: 0
Views: 468
Reputation: 34979
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.
Upvotes: 1