Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 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 Answer

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%<>" ^%!%<> ^

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...