如何在.bat文件中正确转义字符串以实现无执行无引号原样输出
If you’ve tried using echo in a Windows batch file to print text containing < or > characters, you’ve likely hit two frustrating issues: either the command throws an error (since those characters are treated as redirection operators), or wrapping the text in quotes fixes the error but leaves unwanted quotes in the output.
The Fix: Use the ^ Escape Character
In Windows batch scripting, ^ works as the escape character—similar to \ in PHP—it tells the command interpreter to treat the following special character as plain text instead of parsing it as a command operator.
To get your desired output without quotes or unintended command execution, just prefix every < and > with ^. Here’s the working batch file code:
@echo off rem Note all < and > are prefixed with a ^ echo Syntax: cmd.cmd -p ^<path^> -c ^<command^> echo commands: mv echo path: ./dirname ^<- no trailing slash
What This Outputs
When you run this batch file, your console will print exactly the content you want—clean, no quotes, no errors:
Syntax: cmd.cmd -p <path> -c <command> commands: mv path: ./dirname <- no trailing slash
内容的提问来源于stack exchange,提问作者Kim Steinhaug

