Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using batch echo with special characters

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" ?> 
like image 810
Amir Zadeh Avatar asked Sep 05 '11 13:09

Amir Zadeh


People also ask

What is @echo off in batch script?

batch-file Echo @Echo off @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.

What does && do in batch file?

&& runs the second command on the line when the first command comes back successfully (i.e. errorlevel == 0 ). The opposite of && is || , which runs the second command when the first command is unsuccessful (i.e. errorlevel != 0 ).


2 Answers

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.

like image 187
Joey Avatar answered Oct 05 '22 11:10

Joey


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.

like image 34
orbitcowboy Avatar answered Oct 05 '22 11:10

orbitcowboy