Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
809 views
in Technique[技术] by (71.8m points)

cmd - Batch: Auto escape special characters

As far as I know, I need to escape every escape characters when echoing them. The ^ method works fine for a few echoes. (which should be something like:)

@echo ^|
@echo ^> ^>^>

However, when there are a lot of characters to escape, the ^ method won't work anymore. So, my question is:

Are there any ways escape all special characters without "spamming" the caret?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As mentioned by Mofi, you could use variables with delayed expansion to echo any content.

There are some more possible ways.

1) Disappearing quotes

for /F %%^" in ("""") do (
  echo %%~"Hello^you
  echo %%~"Line2 |&<>
)

This works as the %~" will quote the rest of the line, but after expansion it will disappear

2) When you want to echo multiple lines you should read about the different variants of heredoc

3) The MagicEcho can display any content without escaping or doubling percent signs like

%magicEcho% "^!%path%<>" ^%!%<> ^

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...