一批回声管道符号导致意外的行为 [英] 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.
推荐答案
有在Windows批处理文件中使用时通常必须进行转义几个特殊字符。以下是部分列表:< > &安培; | ^%
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"
这篇关于一批回声管道符号导致意外的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!