Use The Caret (^) To Escape Characters In The Command Prompt And Batch Files

The Windows command prompt has several “reserved” characters it uses for special functions. These include the greater than (>) for sending output to a file, less than (<) for receiving input from a file and pipe (|) for sending output to ‘chained’ commands. Because of this, you cannot simply use them in commands without either putting them in quotes or escaping them using carat (^) symbol.

There are occasions when you may need to output these special characters and putting them in quotes is not an option. For example, consider generating HTML using this command:

ECHO The source computer name is <b>%ComputerName%</b>

If you run this from the command line, you will get an error because it processes the first ‘<’ symbol as a “receive input” command. Instead, escape it using the carat symbol:

ECHO The source computer name is ^<b^>%ComputerName%^</b^>

Which will properly generate the text:

The source computer name is <b>JASON</b>

You could then send this to a text file:

ECHOThe source computer name is ^<b^>%ComputerName%^</b^> >> webpage.html

While you may seldom have the need for this, it is a good tidbit to know if you do a lot of work from the command line and/or batch scripts.

Free eBook!

Like what you read?

If so, please join over 28,000 people who receive our exclusive weekly newsletter and computer tips, and get FREE COPIES of 5 eBooks we created, as our gift to you for subscribing. Just enter your name and email below:

Post A Comment Using Facebook

What’s Your Preference?

Daily Alerts

Each day we send out a quick email to thousands of PCMECH readers to notify them of new posts. This email is just a short, plain email with titles and links to our latest posts. You can unsubscribe from this service at any time.

You can subscribe to it by leaving your email address in the following field and confirming your subscription when you get an email asking you to do so.

Enter your email address for
Daily Updates:

Weekly Newsletter

Running for over 6 years, the PCMECH weekly newsletter helps you keep tabs on the world of tech. Each issue includes news bits, an article, an exclusive rant as well as a download of the week. This newsletter is subscribed to by over 28,000 readers (many who also subscribe to the other option) - come join the community!

To subscribe to this weekly newsletter simply add your email address to the following field and then follow the confirmation prompts. You will be able to unsubscribe at any time.

Enter your email address for
Free Weekly Newsletter: