How do I escape ampersands in batch files?

133,637

Solution 1

From a cmd:

  • & is escaped like this: ^& (based on @Wael Dalloul's answer)
  • % does not need to be escaped

An example:

start http://www.google.com/search?client=opera^&rls=en^&q=escape+ampersand%20and%20percentage+in+cmd^&sourceid=opera^&ie=utf-8^&oe=utf-8

From a batch file

  • & is escaped like this: ^& (based on @Wael Dalloul's answer)
  • % is escaped like this: %% (based on the OPs update)

An example:

start http://www.google.com/search?client=opera^&rls=en^&q=escape+ampersand%%20and%%20percentage+in+batch+file^&sourceid=opera^&ie=utf-8^&oe=utf-8

Solution 2

& is used to separate commands. Therefore you can use ^ to escape the &.

Solution 3

You can enclose it in quotes, if you supply a dummy first argument.

Note that you need to supply a dummy first argument in this case, as start will treat the first argument as a title for the new console windows, if it is quoted. So the following should work (and does here):

start "" "http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8"

Solution 4

explorer "http://www.google.com/search?client=opera&rls=...."

Solution 5

The command

echo this ^& that

works as expected, outputing

this & that

The command

echo this ^& that > tmp

also works, writing the string to file "tmp". However, before a pipe

echo this ^& that | clip

the ^ is interpreted completely differently. It tries to write the output of the two commands "echo this" and "that" to the pipe. The echo will work then "that" will give an error. Saying

echo this ^& echo that | clip

will put the strings "this" and "that" on the clipboard.

Without the ^:

echo this & echo that | clip

the first echo will write to the console and only the second echo's output will be piped to clip (similarly for "> tmp" redirection). So, when output is being redirected, the ^ does not quote the & but instead causes it to be applied before the redirection rather than after.

To pipe an &, you have to quote it twice

echo this ^^^& that | clip

If you put the string in a variable

set m=this ^& that

then

set m

will output

m=this & that

but the obvious

echo %m%

fails because, after Windows substitutes the variable, resulting in

echo this & that

it parses this as a new command and tries to execute "that".

In a batch file, you can use delayed expansion:

setlocal enableDelayedExpansion
echo !m!

To output to a pipe, we have to replace all &s in the variable value with ^&, which we can do with the %VAR:FROM=TO% syntax:

echo !m:^&=^^^&! | clip

On the command line, "cmd /v" enables delayed expansion:

cmd /v /c echo !m!

This works even when writing to a pipe

cmd /v /c echo !m! | clip

Simple.

Share:
133,637

Related videos on Youtube

Peter Mortensen
Author by

Peter Mortensen

Experienced application developer. Software Engineer. M.Sc.E.E. C++ (10 years), software engineering, .NET/C#/VB.NET (12 years), usability testing, Perl, scientific computing, Python, Windows/Macintosh/Linux, Z80 assembly, CAN bus/CANopen. Contact I can be contacted through this reCAPTCHA (requires JavaScript to be allowed from google.com and possibly other(s)). Make sure to make the subject specific (I said: specific. Repeat: specific subject required). I can not stress this enough - 90% of you can not compose a specific subject, but instead use some generic subject. Use a specific subject, damn it! You still don't get it. It can't be that difficult to provide a specific subject to an email instead of a generic one. For example, including meta content like "quick question" is unhelpful. Concentrate on the actual subject. Did I say specific? I think I did. Let me repeat it just in case: use a specific subject in your email (otherwise it will no be opened at all). Selected questions, etc.: End-of-line identifier in VB.NET? How can I determine if a .NET assembly was built for x86 or x64? C++ reference - sample memmove The difference between + and & for joining strings in VB.NET Some of my other accounts: Careers. [/]. Super User (SU). [/]. Other My 15 minutes of fame on Super User My 15 minutes of fame in Denmark Blog. Sample: Jump the shark. LinkedIn @PeterMortensen (Twitter) Quora GitHub Full jump page (Last updated 2021-11-25)

Updated on February 19, 2021

Comments

  • Peter Mortensen
    Peter Mortensen about 3 years

    How do I escape ampersands in a batch file (or from the Windows command line) in order to use the start command to open web pages with ampersands in the URL?

    Double quotes will not work with start; this starts a new command-line window instead.

    Update 1: Wael Dalloul's solution works. In addition, if there are URL encoded characters (e.g. space is encoded as %20) in the URL and it is in a batch file then '%' must be encoded as '%%'. This is not the case in the example.

    Example, from the command line (CMD.EXE):

    start http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8
    

    will result in

    http://www.google.com/search?client=opera 
    

    being opened in the default browser and these errors in the command line window:

    'rls' is not recognized as an internal or external command,
    operable program or batch file.
    'q' is not recognized as an internal or external command,
    operable program or batch file.
    'sourceid' is not recognized as an internal or external command,
    operable program or batch file.
    'ie' is not recognized as an internal or external command,
    operable program or batch file.
    'oe' is not recognized as an internal or external command,
    operable program or batch file.
    

    Platform: Windows XP 64 bit SP2.

    • william
      william about 12 years
      What about plus sign + what should I put in front to escape it?
    • phuclv
      phuclv almost 7 years
      In PowerShell start "http://www.google.com/search?client=opera&rls=en&q=escape+a‌​mpersand&sourceid=op‌​era&ie=utf-8&oe=utf-‌​8" works because PowerShell will strip quotes out
  • Peter Mortensen
    Peter Mortensen over 14 years
    OK, I will admit the title does not completely match the question inside. (Do you think I should make the title longer?)
  • belugabob
    belugabob over 14 years
    Not so 'Doh', after all - enclosing in quotes was the right idea - just didn't know about the console window title thing. Thanks Johannes!
  • william
    william about 12 years
    What about plus sign + what should I put in front to escape it?
  • Peter Mortensen
    Peter Mortensen over 10 years
    This is the more general applicable solution. It also works if a parameter of a command-line program has one or more ampersands in it. I just encountered this - the parameter was CGW5COMM&10C4&8301.
  • Derek Greer
    Derek Greer over 9 years
    Quoting an ampersand with the caret character ^ doesn't seem to work in all circumstances. For example, attempting to execute msdeploy.exe with the /p: switch to pass a password with an ampersand doesn't seem to work with any permutation I've tried (caret quote, double quotes, dummy first argument).
  • jpmc26
    jpmc26 over 9 years
    An example of using ^ would be invaluable.
  • Lasse Christiansen
    Lasse Christiansen over 9 years
    @jpmc26 I have added an example in this answer.
  • Peter Mortensen
    Peter Mortensen over 9 years
    It could also include an example of escaping "%" - then I can clean up my question.
  • Lasse Christiansen
    Lasse Christiansen over 9 years
    @PeterMortensen Definitely - I have updated my answer.
  • liquide
    liquide over 8 years
    Which characters should I also escape? I noticed that I have to escape "|" by "^|".
  • jeb
    jeb about 8 years
    Your solution for pipes works, but your conclusions are more or less wrong. Btw. a simple echo this ^^^& that | clip works too. To understand the magic pipe handling you could read SO: Why does delayed expansion fail when inside a piped block of code?
  • phuclv
    phuclv almost 7 years
    PowerShell will strip quotes from the arguments, so this will fail. You need to use only start "http://www.google.com/search?client=opera&rls=en&q=escape+a‌​mpersand&sourceid=op‌​era&ie=utf-8&oe=utf-‌​8" on PowerShell
  • Peter Mortensen
    Peter Mortensen over 5 years
  • Peter Mortensen
    Peter Mortensen over 5 years
    E.g from the second source: "The way cmd.exe interprets the command line is different and completely independent of how an executable program interprets the command line."
  • Peter Mortensen
    Peter Mortensen over 5 years
    ^ does not work for escaping %. Use % instead: %%
  • Jeremy
    Jeremy over 5 years
    @PeterMortensen the recaptcha link in your profile is dead
  • TamaMcGlinn
    TamaMcGlinn about 4 years
    In this case % does not need to be escaped, because who would be silly enough to define 20and as an environment variable? If you did, start http://www.google.com/search?q=ampersand%20and%20percentage could link tohttp://www.google.com/search?q=ampersandsomething-silly20p‌​ercentage instead. Using ^% to escape in cmd, the example works as expected.
  • tripleee
    tripleee about 3 years
    Not my downvote, but this seems to duplicate a couple of older answers, both with more details and explanations.