| Special or Difficult Characters |
The following characters have special meaning and may need careful handling in your batch files:
| Character | Details |
|---|---|
| | | A single vertical bar character (above "\" on keyboard) is used to indicate piping of one programs output into another. |
| < and > | These two characters indicate redirection. ">>" means append to the file you are directing to. |
| & |
A single ampersand character is used to
separate multiple commands on a single line.
This also allows it to be used as follows: set EndsWithFiveSpaces=Something &rem The 5 spaces before the ampersand are significant It is also used in redirection, for example "2>&1" means send stderr output to the same place that stdout is going to. |
| ^ |
The "hat" character is used to escape the
"|", "<", ">" and "&"
characters if required (it can also be used to escape itself).
This will only be required if they are not within double quotes: ECHO This ^& that ECHO "This & that" ECHO "This & that" ^& This Again If you use a hat when its not required it will be kept so you need to be careful. |
| * |
The asterisk character can be used in the "SET" command
but it has special meaning there and so is the only difficult character
for that particular command :-)
The "ReplaceAsterisks() example shows how you can replace this character if required. |
| % (percent) and ! (exclamation mark) |
These characters are used to perform parameter and environment variable
expansion (retrieval).
The ! character only has special meaning when "delayed expansion (ENABLEDELAYEDEXPANSION)" is on. This can be very handy if you are passed the name of a variable and you wish to obrain its contents. To escape "%" use two ("%%) and for "!" use "^!" when within double quotes otherwise "^^!" :-) @echo off setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION set Something=This is the value set VarName=Something echo %VarName% = !%VarName%! echo I want to display this: "%%" and this: '%%' echo I want to display this: "^!" and this: '^^!' |
![]() | ![]() |