Reputation: 31589
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.
Upvotes: 185
Views: 153329
Reputation: 400
For special characters like '&' you can surround the entire expression with quotation marks
set "url=https://url?retry=true&w=majority"
Upvotes: 0
Reputation: 4876
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
Upvotes: 2
Reputation: 75
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.
Upvotes: 0
Reputation: 2412
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.
Upvotes: 19
Reputation: 10325
From a cmd:
&
is escaped like this: ^&
(based on @Wael Dalloul's answer)%
does not need to be escapedAn 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
Upvotes: 134
Reputation: 22984
&
is used to separate commands. Therefore you can use ^
to escape the &
.
Upvotes: 171
Reputation: 4360
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"
Upvotes: 35
Reputation: 94625
explorer "http://www.google.com/search?client=opera&rls=...."
Upvotes: 21