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
561 views
in Technique[技术] by (71.8m points)

windows - Escaping parentheses within parentheses for batch file

This is what I am trying to do:

(
@echo This is some code that is
@echo Important to echo exactly as-is
@echo Even if I use parenthesis
@echo for something (like this)
)|clip

So having that copied into clipboard. I have tried the following:

(
@echo This is some code that is
@echo Important to echo exactly as-is
@echo Even if I use parenthesis
@echo for something ^(like this^)
)|clip

But this seems to affect my WANTED parentheses as well because I receive an error. To test this, I simply done:

(
@echo This is some code that is
@echo Important to echo exactly as-is
@echo Even if I use parenthesis
@echo for something ^(like this^)
FFF
)|clip

Hey! This almost works, the text is copied all the way to the "s" in "this". It does not copy the last parentheses, or obviously the F's. Any ideas?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

It's obvious that you need three carets there :-)

(
@echo This is some code that is
@echo Important to echo exactly as-is
@echo Even if I use parenthesis
@echo for something (like this^^^)
)|clip

Why? That is a bit tricky...

First the parser parses the block and escapes the part this^^^) to this^), the parenthesis was escaped here the first time.

But as you used a pipe, the complete block will be translated and transfered to a new cmd.exe instance.
C:Windowssystem32cmd.exe /S /D /c" ( @ echo This is some code is ... & @ echo for something (like this^) )"

And in the new instance it is necessary again to escape the closing parenthesis.
That's all!

And for more information you could read a similar question
SO: why does delayed expansion fail when inside a piped block of code


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

57.0k users

...