183

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.

3
  • 1
    I've edited belugabob's answer so it should work now. It's just a quirk in start which causes quoting the argument to fail if applied without thought. And overall I think enclosing the argument in quotes is easier and less error-prone than to escape every character that needs escaping in there.
    – Joey
    Commented Aug 25, 2009 at 11:42
  • 1
    What about plus sign + what should I put in front to escape it?
    – william
    Commented Mar 1, 2012 at 3:40
  • In PowerShell start "http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8" works because PowerShell will strip quotes out
    – phuclv
    Commented May 19, 2017 at 5:25

8 Answers 8

168

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

9
  • 4
    What about plus sign + what should I put in front to escape it?
    – william
    Commented Mar 1, 2012 at 3:40
  • 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. Commented Oct 9, 2013 at 13:23
  • 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). Commented Aug 12, 2014 at 13:38
  • 6
    An example of using ^ would be invaluable.
    – jpmc26
    Commented Jan 2, 2015 at 21:42
  • 2
    @jpmc26 I have added an example in this answer. Commented Jan 15, 2015 at 10:02
133

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
4
  • It could also include an example of escaping "%" - then I can clean up my question. Commented Jan 15, 2015 at 18:53
  • @PeterMortensen Definitely - I have updated my answer. Commented Jan 16, 2015 at 7:38
  • 2
    Which characters should I also escape? I noticed that I have to escape "|" by "^|".
    – liquide
    Commented Oct 7, 2015 at 10:44
  • 1
    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-silly20percentage instead. Using ^% to escape in cmd, the example works as expected. Commented Apr 1, 2020 at 9:22
35

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"
3
  • OK, I will admit the title does not completely match the question inside. (Do you think I should make the title longer?) Commented Aug 25, 2009 at 10:57
  • Not so 'Doh', after all - enclosing in quotes was the right idea - just didn't know about the console window title thing. Thanks Johannes!
    – belugabob
    Commented Aug 25, 2009 at 11:46
  • 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+ampersand&sourceid=opera&ie=utf-8&oe=utf-8" on PowerShell
    – phuclv
    Commented May 19, 2017 at 5:27
21
explorer "http://www.google.com/search?client=opera&rls=...."
0
18

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.

1
1

If you need to echo a string that contains an ampersand, quotes won't help, because you would see them on the output as well. In such a case, use for:

for %a in ("First & Last") do echo %~a

...in a batch script:

for %%a in ("First & Last") do echo %%~a

or

for %%a in ("%~1") do echo %%~a
0

If you have spaces in the name of the file and you have a character you need to escape:

You can use single AND double quotes to avoid any misnomers in the command.

scp ./'files name with spaces/internal folder with spaces/"text & files stored.txt"' .

The ^ character escapes the quotes otherwise.

0

For special characters like '&' you can surround the entire expression with quotation marks

set "url=https://url?retry=true&w=majority"

1
  • Not my downvote, but this seems to duplicate a couple of older answers, both with more details and explanations.
    – tripleee
    Commented Feb 6, 2021 at 12:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.