using batch echo with special characters

200,301

Solution 1

You can escape shell metacharacters with ^:

echo ^<?xml version="1.0" encoding="utf-8" ?^> > myfile.xml

Note that since echo is a shell built-in it doesn't follow the usual conventions regarding quoting, so just quoting the argument will output the quotes instead of removing them.

Solution 2

In order to use special characters, such as '>' on Windows with echo, you need to place a special escape character before it.

For instance

echo A->B

will not work since '>' has to be escaped by '^':

 echo A-^>B

See also escape sequences. enter image description here

There is a short batch file, which prints a basic set of special character and their escape sequences.

Solution 3

another method:

@echo off

for /f "useback delims=" %%_ in (%0) do (
  if "%%_"=="___ATAD___" set $=
  if defined $ echo(%%_
  if "%%_"=="___DATA___" set $=1
)
pause
goto :eof

___DATA___
<?xml version="1.0" encoding="utf-8" ?>
 <root>
   <data id="1">
      hello world
   </data>
 </root>
___ATAD___


rem # 
rem # 

Solution 4

One easy solution is to use delayed expansion, as this doesn't change any special characters.

set "line=<?xml version="1.0" encoding="utf-8" ?>"
setlocal EnableDelayedExpansion
(
  echo !line!
) > myfile.xml

EDIT : Another solution is to use a disappearing quote.

This technic uses a quotation mark to quote the special characters

@echo off
setlocal EnableDelayedExpansion
set ""="
echo !"!<?xml version="1.0" encoding="utf-8" ?>

The trick works, as in the special characters phase the leading quotation mark in !"! will preserve the rest of the line (if there aren't other quotes).
And in the delayed expansion phase the !"! will replaced with the content of the variable " (a single quote is a legal name!).

If you are working with disabled delayed expansion, you could use a FOR /F loop instead.

for /f %%^" in ("""") do echo(%%~" <?xml version="1.0" encoding="utf-8" ?>

But as the seems to be a bit annoying you could also build a macro.

set "print=for /f %%^" in ("""") do echo(%%~""

%print%<?xml version="1.0" encoding="utf-8" ?>
%print% Special characters like &|<>^ works now without escaping

Solution 5

The way to output > character is to prepend it with ^ escape character:

echo ^>

will print simply

>
Share:
200,301

Related videos on Youtube

Amir Zadeh
Author by

Amir Zadeh

Updated on July 08, 2022

Comments

  • Amir Zadeh
    Amir Zadeh almost 2 years

    This maybe really easy but there were no answers for it over the net. I want to echo a XML line via batch into a file but it misunderstands the XML closing tag for redirection ">". The line is as follows:

    echo <?xml version="1.0" encoding="utf-8" ?> > myfile.xml
    

    is there any way to give a hint to batch parser not to interpret a special string? I used double-quotes but it writes them to the file as well! The file should look like this after echo:

    <?xml version="1.0" encoding="utf-8" ?>
    
  • Rich
    Rich over 9 years
    @Nevin: You still have to escape each and every < and >. Nothing special there. That being said, I'd advise against echoing large XML files on a single line for ... kinda obvious reasons.
  • Nevin Raj Victor
    Nevin Raj Victor over 9 years
    :Sorry to ask again..It's not working for me... Code is given below. Sorry for not giving the code in correct format echo ^<?xml version="1.0" encoding="utf-8"?^> ^<configuration^> ^<packageSources^> ^<add key="nuget.org" value="nuget.org/api/v2/"; /^> ^<add key="aspnetwebstacknightlyrelease" value="myget.org/f/aspnetwebstacknightlyrelease/"; /^> ^</packageSources^> ^</configuration^> >MyFile.xml – Nevin Raj 1 hour ago
  • Rich
    Rich over 9 years
    @Nevin: There are semicolons in there that should not be there. Apart from that it produces valid XML for me.
  • Nevin Raj Victor
    Nevin Raj Victor over 9 years
    You were absolutely correct..The semicolon caused the trouble..Thanks
  • Rich C
    Rich C about 9 years
    Not needing to batch very often, I keep forgetting my top 5 illegal echo characters: <, >, ', ( and ). All can be delimited with a caret ^. More illegal characters here but most of them aren't in my typical use case.
  • Davor Josipovic
    Davor Josipovic almost 9 years
    echo !line! will work as long as there are no ! in the line variable. Any remedy for that?
  • jeb
    jeb almost 9 years
    No, if line contains a ! then it will work without any problems, but it's not always obvious how to get the exclamation marks into the variable. Try set "line=Hello^!" & echo !line!
  • Davor Josipovic
    Davor Josipovic almost 9 years
    you are 100% correct. I must have looked over something!
  • jeb
    jeb over 8 years
    But for quotes, the caret is the escape character, using two quotes is wrong, as it adds simply a second quote. echo Line1"" & echo line2 vs echo Line1^" & echo line2
  • Ohad Schneider
    Ohad Schneider almost 7 years
    Note that this preserves the quotes in the output
  • yO_
    yO_ over 5 years
    Nice idea, to add data in a script!
  • PMF
    PMF over 4 years
    Nice solution to unpack a text file from a batch file. The line if defined $ echo(%%_ should be extended to if defined $ echo(%%_ >> myxmlfile.xml to copy the output into a file.
  • bilogic
    bilogic almost 4 years
    can we have variables inside __DATA__?
  • Stephan
    Stephan almost 4 years
    the question is about batch/cmd. This surely won't work in batch/cmd.
  • Ben
    Ben over 3 years
    Thanks for providing the table of examples. It helped me with multiple characters.