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.
This question is related to
windows
batch-file
cmd
escaping
ampersand
explorer "http://www.google.com/search?client=opera&rls=...."
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.
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"
For special characters like '&' you can surround the entire expression with quotation marks
set "url=https://url?retry=true&w=majority"
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
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.
&
is used to separate commands. Therefore you can use ^
to escape the &
.
Source: Stackoverflow.com