175

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.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 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 Aug 25 '09 at 11:42
  • 1
    What about plus sign `+` what should I put in front to escape it? – william Mar 01 '12 at 03: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](https://superuser.com/a/1208974/241386) – phuclv May 19 '17 at 05:25

8 Answers8

163

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wael Dalloul
  • 22,172
  • 11
  • 48
  • 57
  • 4
    What about plus sign `+` what should I put in front to escape it? – william Mar 01 '12 at 03: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*. – Peter Mortensen Oct 09 '13 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). – Derek Greer Aug 12 '14 at 13:38
  • 6
    An example of using `^` would be invaluable. – jpmc26 Jan 02 '15 at 21:42
  • 2
    @jpmc26 I have added an example [in this answer](http://stackoverflow.com/a/27960888/700926). – Lasse Christiansen Jan 15 '15 at 10:02
  • A more general treatment of this subject is in *[A Better Way To Understand Quoting and Escaping of Windows Command Line Arguments](http://www.windowsinspired.com/understanding-the-command-line-string-and-arguments-received-by-a-windows-program/)*, *[How a Windows Program Splits Its Command Line Into Individual Arguments](http://www.windowsinspired.com/how-a-windows-programs-splits-its-command-line-into-individual-arguments/)*, and *[How Command Line Parameters Are Parsed](http://daviddeley.com/autohotkey/parameters/parameters.htm)*. – Peter Mortensen Nov 02 '18 at 12:00
  • 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 Nov 02 '18 at 12:01
  • `^` does not work for escaping `%`. Use `%` instead: `%%` – Peter Mortensen Nov 16 '18 at 11:52
  • @PeterMortensen the recaptcha link in your profile is dead – Jeremy Nov 16 '18 at 20:09
127

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
Community
  • 1
  • 1
Lasse Christiansen
  • 10,205
  • 7
  • 50
  • 79
  • It could also include an example of escaping "%" - then I can clean up my question. – Peter Mortensen Jan 15 '15 at 18:53
  • @PeterMortensen Definitely - I have updated my answer. – Lasse Christiansen Jan 16 '15 at 07:38
  • 2
    Which characters should I also escape? I noticed that I have to escape "|" by "^|". – liquide Oct 07 '15 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 to`http://www.google.com/search?q=ampersandsomething-silly20percentage` instead. Using `^%` to escape in cmd, the example works as expected. – TamaMcGlinn Apr 01 '20 at 09:22
32

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"
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
belugabob
  • 4,302
  • 22
  • 22
  • OK, I will admit the title does not completely match the question inside. (Do you think I should make the title longer?) – Peter Mortensen Aug 25 '09 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 Aug 25 '09 at 11:46
  • [PowerShell will strip quotes from the arguments](https://superuser.com/a/1208974/241386), 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 May 19 '17 at 05:27
20
explorer "http://www.google.com/search?client=opera&rls=...."
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
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.

Denis Howe
  • 2,092
  • 1
  • 23
  • 25
  • 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?](http://stackoverflow.com/q/8192318/463115) – jeb Feb 23 '16 at 13:20
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jaroslav Záruba
  • 4,694
  • 5
  • 39
  • 58
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
be-ns
  • 75
  • 1
  • 8
0

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

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

Stan
  • 400
  • 3
  • 10
  • Not my downvote, but this seems to duplicate a couple of older answers, both with more details and explanations. – tripleee Feb 06 '21 at 12:43