batch echo pipe symbol causing unexpected behaviour

后端 未结 4 560
旧巷少年郎
旧巷少年郎 2020-12-06 09:37

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.

相关标签:
4条回答
  • 2020-12-06 09:59

    You must escape the | character before you print the var. The following prints a|b

    @echo off
    
    set x=a^|b
    echo %x:|=^|%
    
    0 讨论(0)
  • 2020-12-06 10:01

    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"
    
    0 讨论(0)
  • 2020-12-06 10:03
    set X=%0^|callset.bat
    set Y=%X:|=_%
    echo %Y%
    echo %X% _ %Y%
    REM activate callset | more
    

    REM and you should have infinite pipe. Break CTRL+C twice Ctrl Break REM prints "The process tried to write to a nonexistent pipe."

    0 讨论(0)
  • 2020-12-06 10:20

    escape it

    echo \|
    

    or wrap in quotes

    echo "|"
    
    0 讨论(0)
提交回复
热议问题