Escaping ampersand (&) in URL in Bash under WSL
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I'm trying to write a Bash function which will take some arguments from the command line and put them in a URL which contains URL parameters (i.e. contains ?
and &
).
If there is only one parameter in the URL, there is no &
, and there is no problem, i.e., if I define the following function:
test()
cmd.exe /c start https://example.com/?foo=$1
and invoke it with test bar
, it opens the URL https://example.com/?foo=bar
in my browser, which is completely correct.
The problem is when I want to add a second URL parameter. I then extend the function as follows:
test()
cmd.exe /c start https://example.com/?foo=$1&baz=$2
but when I invoke it with test bar qux
, the same URL as before (https://example.com/?foo=bar
) is opened in my browser, and my terminal displays the error 'baz' is not recognized as an internal or external command, operable program or batch file.
Wrapping the URL in double quotes also does not help: when I change it to
test()
cmd.exe /c start "https://example.com/?foo=$1&baz=$2"
it opens the URL https://example.com//?foo=bar"
, and I still get the error 'baz' is not recognized as an internal or external command, operable program or batch file.
bash shell-script shell windows-subsystem-for-linux
add a comment |Â
up vote
0
down vote
favorite
I'm trying to write a Bash function which will take some arguments from the command line and put them in a URL which contains URL parameters (i.e. contains ?
and &
).
If there is only one parameter in the URL, there is no &
, and there is no problem, i.e., if I define the following function:
test()
cmd.exe /c start https://example.com/?foo=$1
and invoke it with test bar
, it opens the URL https://example.com/?foo=bar
in my browser, which is completely correct.
The problem is when I want to add a second URL parameter. I then extend the function as follows:
test()
cmd.exe /c start https://example.com/?foo=$1&baz=$2
but when I invoke it with test bar qux
, the same URL as before (https://example.com/?foo=bar
) is opened in my browser, and my terminal displays the error 'baz' is not recognized as an internal or external command, operable program or batch file.
Wrapping the URL in double quotes also does not help: when I change it to
test()
cmd.exe /c start "https://example.com/?foo=$1&baz=$2"
it opens the URL https://example.com//?foo=bar"
, and I still get the error 'baz' is not recognized as an internal or external command, operable program or batch file.
bash shell-script shell windows-subsystem-for-linux
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to write a Bash function which will take some arguments from the command line and put them in a URL which contains URL parameters (i.e. contains ?
and &
).
If there is only one parameter in the URL, there is no &
, and there is no problem, i.e., if I define the following function:
test()
cmd.exe /c start https://example.com/?foo=$1
and invoke it with test bar
, it opens the URL https://example.com/?foo=bar
in my browser, which is completely correct.
The problem is when I want to add a second URL parameter. I then extend the function as follows:
test()
cmd.exe /c start https://example.com/?foo=$1&baz=$2
but when I invoke it with test bar qux
, the same URL as before (https://example.com/?foo=bar
) is opened in my browser, and my terminal displays the error 'baz' is not recognized as an internal or external command, operable program or batch file.
Wrapping the URL in double quotes also does not help: when I change it to
test()
cmd.exe /c start "https://example.com/?foo=$1&baz=$2"
it opens the URL https://example.com//?foo=bar"
, and I still get the error 'baz' is not recognized as an internal or external command, operable program or batch file.
bash shell-script shell windows-subsystem-for-linux
I'm trying to write a Bash function which will take some arguments from the command line and put them in a URL which contains URL parameters (i.e. contains ?
and &
).
If there is only one parameter in the URL, there is no &
, and there is no problem, i.e., if I define the following function:
test()
cmd.exe /c start https://example.com/?foo=$1
and invoke it with test bar
, it opens the URL https://example.com/?foo=bar
in my browser, which is completely correct.
The problem is when I want to add a second URL parameter. I then extend the function as follows:
test()
cmd.exe /c start https://example.com/?foo=$1&baz=$2
but when I invoke it with test bar qux
, the same URL as before (https://example.com/?foo=bar
) is opened in my browser, and my terminal displays the error 'baz' is not recognized as an internal or external command, operable program or batch file.
Wrapping the URL in double quotes also does not help: when I change it to
test()
cmd.exe /c start "https://example.com/?foo=$1&baz=$2"
it opens the URL https://example.com//?foo=bar"
, and I still get the error 'baz' is not recognized as an internal or external command, operable program or batch file.
bash shell-script shell windows-subsystem-for-linux
asked Jun 6 at 12:40
Jamy Mahabier
212
212
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Since cmd.exe
is invoked with a command, the cmd.exe
escape syntax needs to be used, rather than the Bash escape syntax, i.e. &
needs to be escaped as ^&
(and ?
does not need escaping). The following works as intended:
test()
cmd.exe /c start "https://example.com/?foo=$1^&baz=$2"
(Note that 'baz' is not recognized as an internal or external command, operable program or batch file.
is an error generated by cmd.exe
: it refers to a batch file, which is a Windows concept.)
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Since cmd.exe
is invoked with a command, the cmd.exe
escape syntax needs to be used, rather than the Bash escape syntax, i.e. &
needs to be escaped as ^&
(and ?
does not need escaping). The following works as intended:
test()
cmd.exe /c start "https://example.com/?foo=$1^&baz=$2"
(Note that 'baz' is not recognized as an internal or external command, operable program or batch file.
is an error generated by cmd.exe
: it refers to a batch file, which is a Windows concept.)
add a comment |Â
up vote
2
down vote
accepted
Since cmd.exe
is invoked with a command, the cmd.exe
escape syntax needs to be used, rather than the Bash escape syntax, i.e. &
needs to be escaped as ^&
(and ?
does not need escaping). The following works as intended:
test()
cmd.exe /c start "https://example.com/?foo=$1^&baz=$2"
(Note that 'baz' is not recognized as an internal or external command, operable program or batch file.
is an error generated by cmd.exe
: it refers to a batch file, which is a Windows concept.)
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Since cmd.exe
is invoked with a command, the cmd.exe
escape syntax needs to be used, rather than the Bash escape syntax, i.e. &
needs to be escaped as ^&
(and ?
does not need escaping). The following works as intended:
test()
cmd.exe /c start "https://example.com/?foo=$1^&baz=$2"
(Note that 'baz' is not recognized as an internal or external command, operable program or batch file.
is an error generated by cmd.exe
: it refers to a batch file, which is a Windows concept.)
Since cmd.exe
is invoked with a command, the cmd.exe
escape syntax needs to be used, rather than the Bash escape syntax, i.e. &
needs to be escaped as ^&
(and ?
does not need escaping). The following works as intended:
test()
cmd.exe /c start "https://example.com/?foo=$1^&baz=$2"
(Note that 'baz' is not recognized as an internal or external command, operable program or batch file.
is an error generated by cmd.exe
: it refers to a batch file, which is a Windows concept.)
answered Jun 6 at 12:44
Jamy Mahabier
212
212
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448195%2fescaping-ampersand-in-url-in-bash-under-wsl%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password