Search code examples
batch-filecmdescapingampersand

How to escape '&' ampersand in command prompt


If you have maybe a code like this

set "CONTENT_LENGTH=24" & echo first=1&second=2&third=3

I want the echo to echo with the &


Solution

  • Two ways you could do this.

    Escape characters

    In Windows batch files it seems to be the ^ character, so you'd just stick a ^ before your special character.

    This page seems to cover the exceptions: http://www.robvanderwoude.com/escapechars.php

    On OS X/Linux, use backslashes: echo three\&four

    Wrap the parameter phrase in quotes.

    echo "three&four"
    

    This should work on either system.