using batch echo with special characters
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.
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
>
Related videos on Youtube
![Amir Zadeh](https://i.stack.imgur.com/bY6mN.jpg?s=256&g=1)
Amir Zadeh
Updated on July 08, 2022Comments
-
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" ?>
-
Helen almost 13 yearspossible duplicate of Escape angle brackets in a Windows command prompt
-
phuclv over 7 yearsPossible duplicate of Batch character escaping
-
phuclv almost 6 yearsPossible duplicate of Escape angle brackets in a Windows command prompt
-
-
Rich over 9 years@Nevin: You still have to escape each and every
<
and>
. Nothing special there. That being said, I'd advise againstecho
ing large XML files on a single line for ... kinda obvious reasons. -
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 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 over 9 yearsYou were absolutely correct..The semicolon caused the trouble..Thanks
-
Rich C about 9 yearsNot 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 almost 9 years
echo !line!
will work as long as there are no!
in theline
variable. Any remedy for that? -
jeb almost 9 yearsNo, 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. Tryset "line=Hello^!" & echo !line!
-
Davor Josipovic almost 9 yearsyou are 100% correct. I must have looked over something!
-
jeb over 8 yearsBut for quotes, the caret is the escape character, using two quotes is wrong, as it adds simply a second quote.
echo Line1"" & echo line2
vsecho Line1^" & echo line2
-
Ohad Schneider almost 7 yearsNote that this preserves the quotes in the output
-
yO_ over 5 yearsNice idea, to add data in a script!
-
PMF over 4 yearsNice solution to unpack a text file from a batch file. The line
if defined $ echo(%%_
should be extended toif defined $ echo(%%_ >> myxmlfile.xml
to copy the output into a file. -
bilogic almost 4 yearscan we have variables inside
__DATA__
? -
Stephan almost 4 yearsthe question is about batch/cmd. This surely won't work in batch/cmd.
-
Ben over 3 yearsThanks for providing the table of examples. It helped me with multiple characters.