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

batch echo pipe symbol causing unexpected behaviour

I have a variable in my batch file and it contains the pipe symbol (this one: |) so when I echo the variable I get an error about a unrecognized internal/external command.

I need a way to either get it to echo it correctly or better yet remove everything after and including the | symbol as well as any extra spaces before it.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

There are several special characters that generally must be escaped when used in Windows batch files. Here is a partial list: < > & | ^ %

The escape character is ^. So to get a literal |, you should do this:

echo ^|

When the special character is in a variable, it becomes a bit harder. But if you use special syntax, you can replace characters in a variable like this:

set X=A^|B

REM replace pipe character with underscore
set Y=%X:|=_%

echo %Y%
REM prints "A_B"

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

...