Sunday 23 June 2019

Batch: Escape Characters


All DOS versions interpret some special characters before executing a command.

Example of Special Characters
%, <, |, >

How to Escape %sign?
Use %% to escape % sign.

Hello.bat
@echo off

set name=Krishna Gurram

echo Printing Actual Name %name
echo Printing the variable name by escaping %%name


When you ran Hello.bat, you can see below messages in console.

C:\Users\Public>Hello
Printing Actual Name name
Printing the variable name by escaping %name

Escaping redirection symbol >
In Windows 95/98 and NT, and OS/2, you can escape redirection symbols by placing them in double quotes.

In Windows NT and OS/2, you can escape special characters by prefixing ^ (carets).

For example,

Hello.bat
@echo off

echo Hello > World

When you ran Hello.bat, you can see nothing in console.

Below program prints redirection symbol by escaping it by placing it in ", using ^ symbol. 



Hello.bat
@echo off

echo Hello ">" World
echo Hello ^> World

When you ran Hello.bat, you can see below messages in console.
C:\Users\Public>Hello
Hello ">" World
Hello > World

Reference




Previous                                                 Next                                                 Home

No comments:

Post a Comment