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" ?>
In order to use special characters, such as '>' on Windows with echo, you need to place a special escape character before it.
For instance
will not work since '>' has to be escaped by '^':
See also escape sequences.
There is a short batch file, which prints a basic set of special character and their escape sequences.
Why not use single quote?
output
One easy solution is to use delayed expansion, as this doesn't change any special characters.
EDIT : Another solution is to use a disappearing quote.
This technic uses a quotation mark to quote the special characters
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.
The answer from Joey was not working for me. After executing
I got this error bash: syntax error near unexpected token `>'
This solution worked for me:
See also http://www.robvanderwoude.com/escapechars.php
You can escape shell metacharacters with
^
: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.another method: