How can I add greater than and less than into a batch file variable

后端 未结 3 1559
忘了有多久
忘了有多久 2020-12-02 02:09

When I try

@echo off
set PTag=^
echo %PTag%

I get nothing.

Now what\'s interesting is that if there\'s an empty line aft

相关标签:
3条回答
  • 2020-12-02 02:44

    Just quote your set:

    set "PTag=^<BR^>"
    echo %PTag%
    
    0 讨论(0)
  • 2020-12-02 02:45

    Just use Delayed Expansion when you show the variable value:

    @echo off
    setlocal EnableDelayedExpansion
    
    set "PTag=<BR>"
    echo !PTag!
    
    0 讨论(0)
  • 2020-12-02 03:11

    set PTag=^<BR^> sets the value <BR> to PTag

    When you run echo %PTag% it expands to echo <BR> which is an invalid redirection. You need to escape the < and > inside PTag by using this

    set PTag=^^^<BR^^^>
    

    The first ^ escapes itself, then the next one escapes < or >

    You can also use this

    set "PTag=^<BR^>"
    

    Reason for the second way: inside quotes ^ loses its special meaning

    If it is a quote (") toggle the quote flag, if the quote flag is active, the following special characters are no longer special: ^ & | < > ( ).

    How does the Windows Command Interpreter (CMD.EXE) parse scripts?

    most special characters (^ & ( ) < > | and also the standard delimiters , ; = SPACE TAB) lose their particular meaning as soon as ther are placed in between "", and the "" themselves do not become part of the variable value

    Special Characters in Batch File


    Now the variable will have the value ^<BR^> inside it, and it'll expand echo %PTag% to

    echo ^<BR^>
    

    which is a valid command

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