简体   繁体   中英

Windows echo command: The odds of cmd.exe escaping

I'm in the unfortunate position to be forced to invoke a program via echo <input> | program.exe 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM