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:
- Always escape special characters using a caret in front of the special character.
- 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:
Thanks for that - it really helped.
thanks for the helpful hint
Indeed, it helped :D
Helped me too
Thanks for the comments guys. I'm really glad people find this helpful :)
Woohoo. Thanks for the help, this has been bugging me for a while.
Thanks... Just what I was looking for.
THANK YOU BIG TIME!!!!!!
Exactly what I was looking for. Thanks!
echo %%PATH%%
Try: Hello World!!
echo Hello World!!
¡Doesn't work!
Okay. This should:
echo Hello World^!^!
¡Neither!
Solution:
echo Hello World^^!^^!
¡¡WEIRD!!
Thanks!
BTW, @Abdiel echo Hello World!! works fine for me. {XP, SP3}
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...
Dude thanks sooooo much i've been trying to find this out for days now!
THANX AS SH**ING LOT! :P
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!!!! :)
Vincenzo, you can use !1394CAMERA! if you enable delayed expansion.
setlocal ENABLEDELAYEDEXPANSION
set 1394CAMERA=Hello
echo !1394CAMERA!
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.
Post a Comment