Search code examples
windowscommand-lineecho

Echoing a variable with a greater than sign (>) in a Windows command prompt causes a file creation, even after using the escape caret (^)


I would like to save the text Hello>World in a variable in a Windows command prompt, and then echo it.

So, I run the following commands, and I am properly escaping the text by using the caret ^>:

set i=Hello^>World
echo %i%

However, the echo statement creates a text file called World in the current directory, and nothing echoes to the command prompt.

How do I properly echo the variable i without creating a file?

See screenshots:

enter image description here

enter image description here


Solution

  • C:\>set i=Hello^^^>World
    
    C:\>echo %i%
    Hello>World
    

    The first caret escapes the second caret, and the third caret escapes the redirect. Then when you substitute the variable, the redirect is still escaped.