Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape angle brackets in a Windows command prompt

I need to echo a string containing angle brackets (< and >) to a file on a Windows machine. Basically what I want to do is the following:
echo some string < with angle > brackets >>myfile.txt

This doesn't work since the command interpreter gets confused with the angle brackets. I could quote the whole string like this:
echo "some string < with angle > brackets" >>myfile.txt

But then I have double quotes in my file that I don't want.

Escaping the brackets ala unix doesn't work either:
echo some string \< with angle \> brackets >>myfile.txt

Ideas?

like image 733
Jason Avatar asked Oct 30 '08 20:10

Jason


People also ask

How do I escape special characters in CMD?

If you need to use any of these characters as part of a command-line argument to be given to a program (for example, to have the find command search for the character >), you need to escape the character by placing a caret (^) symbol before it.

What do the angle brackets in a Command Prompt indicate?

The angle brackets ( < > ) indicate that the enclosed element (parameter, value, or information) is mandatory. You are required to replace the text within the angle brackets with the appropriate information. Do not type the angle brackets themselves in the command line.

How do I use brackets in CMD?

The square brackets ( [ ] ) indicate that the enclosed element (parameter, value, or information) is optional. You can choose one or more items or no items. Do not type the square brackets themselves in the command line.

What is the escape character in Windows?

Escape sequences allow you to send nongraphic control characters to a display device. For example, the ESC character (\033) is often used as the first character of a control command for a terminal or printer. Some escape sequences are device-specific.


2 Answers

The Windows escape character is ^, for some reason.

echo some string ^< with angle ^> brackets >>myfile.txt 
like image 138
Tim Robinson Avatar answered Sep 18 '22 06:09

Tim Robinson


True, the official escape character is ^, but be careful because sometimes you need three ^ characters. This is just sometimes:

C:\WINDOWS> echo ^<html^> <html>  C:\WINDOWS> echo ^<html^> | sort The syntax of the command is incorrect.  C:\WINDOWS> echo ^^^<html^^^> | sort <html>  C:\WINDOWS> echo ^^^<html^^^> ^<html^> 

One trick out of this nonsense is to use a command other than echo to do the output and quote with double quotes:

C:\WINDOWS> set/p _="<html>" <nul <html> C:\WINDOWS> set/p _="<html>" <nul | sort <html> 

Note that this will not preserve leading spaces on the prompt text.

like image 38
sin3.14 Avatar answered Sep 21 '22 06:09

sin3.14