Thursday, January 17, 2008

Escape Characters

I know this is going to be a confusing year, because my first post of this year is on a topic I don't really understand.

For some time now I have been confused what the correct method to escape certain characters from being interpreted when I try to print them out.  Let's say I want to print this line:

< & >

I can't just say

echo < & >

Because I'll get an error that way (try it yourself if you don't believe me).  I have to escape those special characters.  To do that I can use the caret (^).

echo ^< ^& ^>

Nice and easy.  But then if I want to print this line:

%SYSTEMDRIVE%

The batch interpreter does something really baffling.  If I'm doing it from command line, I can escape it with a caret like other special characters.

echo ^%SYSTEMDRIVE^%

But it I do it from inside a batch file, that no longer works.  I have to escape the % not with a caret, but with another %.

echo %%SYSTEMDRIVE%

Confuse the hell out of me, I tell you.  So now I live by these rules:

  1. Always escape special characters using a caret in front of the special character.
  2. Except when you are in a batch file and need to escape a %, then use a %%.

I hope that helps some of you.  Oh wait, nobody reads this blog but me.  Oh, well.

16 comments:

edporteous said...

Thanks for that - it really helped.

slow tick said...

thanks for the helpful hint

Unknown said...

Indeed, it helped :D

Unknown said...

Helped me too

Arif Sukoco said...

Thanks for the comments guys. I'm really glad people find this helpful :)

Faux said...

Woohoo. Thanks for the help, this has been bugging me for a while.

Rahul said...

Thanks... Just what I was looking for.

chalc said...

THANK YOU BIG TIME!!!!!!

Preston said...

Exactly what I was looking for. Thanks!

echo %%PATH%%

Abdiel said...

Try: Hello World!!
echo Hello World!!
¡Doesn't work!
Okay. This should:
echo Hello World^!^!
¡Neither!

Solution:
echo Hello World^^!^^!

¡¡WEIRD!!

Vivek said...

Thanks!

BTW, @Abdiel echo Hello World!! works fine for me. {XP, SP3}

Anonymous said...

Thanks! Here's a question I am trying to write a string subsitution and my string contains an equals character which throws out my substitution. Example:

where %min% = 999

set str=!str:initial-heap-size="100M"=initial-heap-size="%min%M"!

It finds my string "initial-heap-size="100M""

But reads everything after the first equals sign as the replacement string. Example:

"100M"=initial-heap-size="999M"="100M"

Escaping equals with caret has no effect...

Unknown said...

Dude thanks sooooo much i've been trying to find this out for days now!

THANX AS SH**ING LOT! :P

Vincenzo said...

hi all
have the following envirnment variable to be used in a batch file: %1394CAMERA%. The problem is that %1 is read as first parameters on the command line.. how can i do?
thanks!!!! :)

Arif Sukoco said...

Vincenzo, you can use !1394CAMERA! if you enable delayed expansion.

setlocal ENABLEDELAYEDEXPANSION
set 1394CAMERA=Hello
echo !1394CAMERA!

Feliponcho said...

I tried to use scape characters inside a replacement statement and it doesn't seem to work. Any idea?

to_remove="c:\somefolder"
old_path=%PATH%
new_path=%old_path:%%to_remove%%=%

What I basically want to do is to remove "c:\somefolder" from the PATH environment variable.

Thanks.