• Anthony Stevens

Escape Characters in Windows’ CMD.EXE

Software

I’m currently working with another developer on how to get random windows shell commands that are stored in a database to actually execute properly on a target system.  The idea is that we’ll Start() a new Process object, and execute the command as arguments.

Check out this screenshot:

Windows Command Shell

What we’re doing here is running the local ECHO command; which  will write out another ECHO command to a batch file which we can then execute.  A couple neat things which may help you:

The caret (^) character is an escape character for things like <, >, (, ), etc.    These are special shell characters.

You can quote the whole string.

I hope this helps the random Google searcher who arrives here.

31 Comments

30 Comments

  1. Jakub Lucký  •  Apr 27, 2008 @10:38 pm

    “I hope this helps the random Google searcher who arrives here.”

    Sure it does ;)

  2. Tabor  •  May 12, 2008 @4:26 pm

    It sure does help.

  3. Jon  •  Jul 19, 2008 @10:59 am

    Does indeed. I tried every combination of forward and backslashes imaginable. A caret? Heh.

  4. Random User  •  Jul 22, 2008 @11:53 pm

    Hi, thank you!

    I just want to confirm your wish… thank you again ^_^

  5. Inceva  •  Aug 20, 2008 @9:33 am

    I alot have tried every combination of forward and backslashes imaginable. Thank you so much for posting. If you ever need any help on a web project hit me up on ww.inceva.tk.

    Thanks!

  6. Wyrd  •  Sep 11, 2008 @9:36 am

    Yes, it does help. *Thank you*.

    The thing is, I think I knew this once, but then I forgot it.

    But what I was really trying to do still doesn’t seem to work.

    I was trying to take output like this:
    ping -n 1 | find “Reply” > 1pingout

    then I wanted to set the window title equal to the result of that ping.

    The way I tried doing it was:
    set /p MYTITLE= < 1pingout
    title %MYTITLE%

    This fails whenever MYTITLE contains a “<”, which it sometimes does if the ping return time was really small.

    Even with all the new options in set, I can’t see any way of escaping the “<” without using something Unixie.


    Furry cows moo and decompress.

  7. Stephan Wack  •  Dec 18, 2008 @1:44 am

    title “%MYTITLE%”
    if the ‘”‘ don’t bother you
    or
    title %MYTITLE:<= less %
    This replaces “<” for ” less “.

  8. asdasda  •  May 18, 2009 @5:33 pm

    thanks!

  9. John McNelly  •  Jun 4, 2009 @1:10 pm

    You can also use ^ as a line continuation character for readability. To escape an embedded ” use a backslash, useful with paths within “c:\Program Files” e.g.:


    REM Create a new windows service, Foo

    sc.exe create Foo ^
    binPath= "\"C:\Program Files\foo.exe\" \"C:\Program Files\foo.ini\"" ^
    displayName= "Hello, world"

    REM Delete the service
    sc.exe delete Foo

  10. He who has been helped!  •  Jun 15, 2009 @5:21 am

    Thank you!!!

    You can add this random googler to your list of those who have been helped and whose lives have been forever changed for the better by learning that ^ is the cmd.exe escape char ;-)

  11. Nate  •  Jul 24, 2009 @6:56 am

    follow-up to John McNelly • Jun 4, 2009 @1:10 pm:

    to see the results of the service creation before you delete it, do

    sc.exe qc Foo

  12. Serge Gautherie  •  Aug 4, 2009 @6:30 pm

    Thanks for your tip!

    Actually, it turned out I needed to escape a ‘%’ in a batch file:
    its escape sequence seems to be “%%^” ;->

  13. Herward Hoyer  •  Sep 15, 2009 @7:12 am

    *Google Random Searcher*
    Thank You!

  14. The Lightning Stalker  •  Oct 31, 2009 @9:52 pm

    It seems to work for anything except ” (double quotes). Unfortunately, that’s what I’m trying to work with :( ( ahh well. It looks like I’ll have to write another FreeBASIC/assembly program.

  15. The Escaped One  •  Nov 13, 2009 @1:40 pm

    Another thank you here. It DID work on double quotes in a “c:\Program Files” situation.

  16. james russell  •  Jan 7, 2010 @4:39 pm

    I have an interesting problem I can’t seem to google up:
    I an using a batch file (that never changes) to run a text file (that changes at my whim), using the following line in the batch:
    for /F “eol=; tokens=2,3* delims=, ” %%i in (TEXT.TXT) do call %%i %%j %%k

    However when it runs the following two lines out of the batch “text”:
    set set server=\\Computer\share\folder\folder2
    echo echo %server%\folder3\Download.zip

    I get:
    \\Computer\share\folder\folder2 \folder3\Download.zip
    I wanted:
    \\Computer\share\folder\folder2\folder3\Download.zip

    Doh!! stupid space!! How do I get this thing to work reliably?
    This is also causing statements like:
    echo echo PRINT THIS TO DOCUMENT >>document.txt
    to fail

    HEEEELLLLLLPPPPPP!!!!

  17. james russell  •  Jan 7, 2010 @6:07 pm

    Well, I don’t know yet if I solved my piping problem, but the trailing space on variables was easy to fix after all. No changes to the batch file, just the executed text file.
    Change the first line to:
    set set server=\\Computer\share\folder\folder2+

    use a neat substitution trick to remove the “+ ” (ie the trailing space):
    echo echo %server:+ =%\folder3\download.zip

    works like this too:
    echo echo “%server:+ =%\folder3\download.zip”

    Bear in mind of course, that what gets echoed has a trailing space :(

  18. Mea Culpa  •  Mar 9, 2010 @11:25 am

    Thanks to John McNelly! ;-)

  19. Anish  •  Mar 30, 2010 @10:57 pm

    Hi,
    How to escape & in comand prompt.

  20. digitale  •  Apr 21, 2010 @3:06 pm

    how to escape space on windows 7?

    Because when i do: “c:\\program files (x86)\\mozilla firefox\\firefox.exe\”

    i says not recognized command c:\program

  21. Laserray  •  Apr 25, 2010 @12:55 pm

    Note: The “^” escape character only works in Windows NT and higher batch scripts. It does not work in Windows 95 batch scripts (and I have not found any way to escape the in Win95). If anyone has a way to do that, please post. Thanks

  22. Laserray  •  Apr 25, 2010 @12:57 pm

    Followup: looks like the posting form eliminated my \. I will try to escape them this time.

  23. Laserray  •  Apr 25, 2010 @12:59 pm

    OK, try the ^:

    ^> and ^<

  24. Ahmet  •  May 7, 2010 @7:21 am

    It definitely helped, thanks a lot ;)

  25. ben  •  Aug 11, 2010 @7:46 am

    This is cool, thanks for posting. I needed to pass a variable to SDTOUT from a batch script, but cmd.exe kept parsing it as a variable. The way to do it is to use %%variable%% in your script.

  26. Alin  •  Jan 29, 2011 @6:22 am

    How do I execute in cmd the following bash command?
    a=`cat test.txt`

    Thanks

  27. csl  •  Jul 5, 2011 @11:38 pm

    Random google searcher says: thanks a lot!

  28. Tonny  •  Jul 26, 2011 @12:05 pm

    Just spend 3 hours at work trying to do an echo containing a sql bulk insert inside a () body of a for. I knew there was an escape char, but for the life of me couldn’t remember which one. And the proxy was down so I couldn’t Google it from the office.

    echo USE my_db > import.sql
    for %%i in (*.csv) do (
    echo BULK INSERT [Table] FROM '%%i' WITH ˆ( FIELDDELIMITER = ";" ˆ) >> import.sql
    echo GO >> import.sql
    )
    osql -U sa -D SQLSERVER -i import.sql
    del *.csv

  29. David Rogers  •  Sep 2, 2011 @1:32 pm

    Huzzah! A line continuation character! And I thought I was just on a wlld bing-chase!

  30. Foo Bar  •  Oct 6, 2011 @2:49 pm

    Post is still helping 3 years later. You just solved my problem with using a greater than sign in Oracle’s ADRCI command line tool:

    c:\>adrci exec="set homepath diag\\tnslsnr\\brucel\\listener; show alert -p \\\"originating_timestamp ^> systimestamp - 1/24\\\" -term"|grep TNS

    Works like a champ now.

1 Trackback