bash escaping quotes
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have an interesting challenge for how to escape quotes in a bash script.
My bash script has a long curl call with a large -d json structure passed.
#!/bin/bash
Value4Variable=Value4
curl -s -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d
'"Variable1":"Value1",
"Variable2":"Value2's", # escape of quote doesnt work because of the previous single quote and backslash
"Variable3":"Value3",
"Variable4":"'"$Value4Variable"'",
"Variable5":"Value5"
'
https://www.hashemian.com/tools/form-post-tester.php
What's the best way to add a single quote into the json structure? I've tried various combinations but no success.
bash scripting quoting escape-characters
add a comment |Â
up vote
1
down vote
favorite
I have an interesting challenge for how to escape quotes in a bash script.
My bash script has a long curl call with a large -d json structure passed.
#!/bin/bash
Value4Variable=Value4
curl -s -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d
'"Variable1":"Value1",
"Variable2":"Value2's", # escape of quote doesnt work because of the previous single quote and backslash
"Variable3":"Value3",
"Variable4":"'"$Value4Variable"'",
"Variable5":"Value5"
'
https://www.hashemian.com/tools/form-post-tester.php
What's the best way to add a single quote into the json structure? I've tried various combinations but no success.
bash scripting quoting escape-characters
2
The same way you used for expanding the variable. End the single-quotes, insert a single-quote (either quoted or escaped), then start the single-quoted string again. So'....'''....'
or'....'"'"'....'
â ilkkachu
Jan 3 at 18:30
1
Why not put the JSON into a data file, and usecurl [...] --data @mydata.json
?
â DopeGhoti
Jan 3 at 18:42
1
@DopeGhoti there is a variable that needs to be expanded inside the json - it won't be expanded inside a file without extra work.
â Michael Daffin
Jan 3 at 18:50
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have an interesting challenge for how to escape quotes in a bash script.
My bash script has a long curl call with a large -d json structure passed.
#!/bin/bash
Value4Variable=Value4
curl -s -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d
'"Variable1":"Value1",
"Variable2":"Value2's", # escape of quote doesnt work because of the previous single quote and backslash
"Variable3":"Value3",
"Variable4":"'"$Value4Variable"'",
"Variable5":"Value5"
'
https://www.hashemian.com/tools/form-post-tester.php
What's the best way to add a single quote into the json structure? I've tried various combinations but no success.
bash scripting quoting escape-characters
I have an interesting challenge for how to escape quotes in a bash script.
My bash script has a long curl call with a large -d json structure passed.
#!/bin/bash
Value4Variable=Value4
curl -s -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d
'"Variable1":"Value1",
"Variable2":"Value2's", # escape of quote doesnt work because of the previous single quote and backslash
"Variable3":"Value3",
"Variable4":"'"$Value4Variable"'",
"Variable5":"Value5"
'
https://www.hashemian.com/tools/form-post-tester.php
What's the best way to add a single quote into the json structure? I've tried various combinations but no success.
bash scripting quoting escape-characters
asked Jan 3 at 18:23
Bernie Lenz
1154
1154
2
The same way you used for expanding the variable. End the single-quotes, insert a single-quote (either quoted or escaped), then start the single-quoted string again. So'....'''....'
or'....'"'"'....'
â ilkkachu
Jan 3 at 18:30
1
Why not put the JSON into a data file, and usecurl [...] --data @mydata.json
?
â DopeGhoti
Jan 3 at 18:42
1
@DopeGhoti there is a variable that needs to be expanded inside the json - it won't be expanded inside a file without extra work.
â Michael Daffin
Jan 3 at 18:50
add a comment |Â
2
The same way you used for expanding the variable. End the single-quotes, insert a single-quote (either quoted or escaped), then start the single-quoted string again. So'....'''....'
or'....'"'"'....'
â ilkkachu
Jan 3 at 18:30
1
Why not put the JSON into a data file, and usecurl [...] --data @mydata.json
?
â DopeGhoti
Jan 3 at 18:42
1
@DopeGhoti there is a variable that needs to be expanded inside the json - it won't be expanded inside a file without extra work.
â Michael Daffin
Jan 3 at 18:50
2
2
The same way you used for expanding the variable. End the single-quotes, insert a single-quote (either quoted or escaped), then start the single-quoted string again. So
'....'''....'
or '....'"'"'....'
â ilkkachu
Jan 3 at 18:30
The same way you used for expanding the variable. End the single-quotes, insert a single-quote (either quoted or escaped), then start the single-quoted string again. So
'....'''....'
or '....'"'"'....'
â ilkkachu
Jan 3 at 18:30
1
1
Why not put the JSON into a data file, and use
curl [...] --data @mydata.json
?â DopeGhoti
Jan 3 at 18:42
Why not put the JSON into a data file, and use
curl [...] --data @mydata.json
?â DopeGhoti
Jan 3 at 18:42
1
1
@DopeGhoti there is a variable that needs to be expanded inside the json - it won't be expanded inside a file without extra work.
â Michael Daffin
Jan 3 at 18:50
@DopeGhoti there is a variable that needs to be expanded inside the json - it won't be expanded inside a file without extra work.
â Michael Daffin
Jan 3 at 18:50
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
There are multiple ways to escape long strings with different quotes. The simplest is to end the single quote and escape the single quote:
'...'''...'
But there are some nicer things you can do. Heredocs are a good way to avoid the quoting issue:
curl -s -X POST https://www.hashemian.com/tools/form-post-tester.php
-H "Content-Type: application/json"
-H "Accept: application/json"
-d @- <<EOF
"Variable1":"Value1",
"Variable2":"Value2's",
"Variable3":"Value3",
"Variable4":"$Value4Variable",
"Variable5":"Value5"
EOF
@-
tell curl to read from stdin and <<EOF
starts the heredoc which will be feed into the stdin of curl. The nice thing with the heredoc is that you do not need to escape any quotes and can use bash variables inside them sidestepping the need to worry about how to escape the quotes and making the whole thing much more readable.
Thanks. The EOF is a really nice approach. I've used it before with ftp commands but I didn't strike the connection that I can use it in curl too.
â Bernie Lenz
Jan 3 at 23:00
add a comment |Â
up vote
1
down vote
I would use --data @filename
notation to read the data from file:
Sample json-data-file
contents:
"Variable1":"Value1",
"Variable2":"Value2's",
"Variable3":"Value3",
"Variable4":"'$Value4Variable'",
"Variable5":"Value5"
The request:
Value4Variable="Value4"
curl -X POST -H "Content-Type: application/json" -H "Accept: application/json"
-d @<(sed "s/$Value4Variable/$Value4Variable/" json-data-file)
https://www.hashemian.com/tools/form-post-tester.php/test123
The output:
The POSTed value is:
********************
"Variable1":"Value1", "Variable2":"Value2's","Variable3":"Value3","Variable4":"'Value4'", "Variable5":"Value5"
********************
Results saved to: test123
*Note: Results are purged periodically. 1 hr minimum life.
Thanks! This works although I prefer to have it all in one file as there are multiple curls in the script and splitting them all into different files makes maintenance a bit more convoluted.
â Bernie Lenz
Jan 3 at 22:57
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
There are multiple ways to escape long strings with different quotes. The simplest is to end the single quote and escape the single quote:
'...'''...'
But there are some nicer things you can do. Heredocs are a good way to avoid the quoting issue:
curl -s -X POST https://www.hashemian.com/tools/form-post-tester.php
-H "Content-Type: application/json"
-H "Accept: application/json"
-d @- <<EOF
"Variable1":"Value1",
"Variable2":"Value2's",
"Variable3":"Value3",
"Variable4":"$Value4Variable",
"Variable5":"Value5"
EOF
@-
tell curl to read from stdin and <<EOF
starts the heredoc which will be feed into the stdin of curl. The nice thing with the heredoc is that you do not need to escape any quotes and can use bash variables inside them sidestepping the need to worry about how to escape the quotes and making the whole thing much more readable.
Thanks. The EOF is a really nice approach. I've used it before with ftp commands but I didn't strike the connection that I can use it in curl too.
â Bernie Lenz
Jan 3 at 23:00
add a comment |Â
up vote
2
down vote
accepted
There are multiple ways to escape long strings with different quotes. The simplest is to end the single quote and escape the single quote:
'...'''...'
But there are some nicer things you can do. Heredocs are a good way to avoid the quoting issue:
curl -s -X POST https://www.hashemian.com/tools/form-post-tester.php
-H "Content-Type: application/json"
-H "Accept: application/json"
-d @- <<EOF
"Variable1":"Value1",
"Variable2":"Value2's",
"Variable3":"Value3",
"Variable4":"$Value4Variable",
"Variable5":"Value5"
EOF
@-
tell curl to read from stdin and <<EOF
starts the heredoc which will be feed into the stdin of curl. The nice thing with the heredoc is that you do not need to escape any quotes and can use bash variables inside them sidestepping the need to worry about how to escape the quotes and making the whole thing much more readable.
Thanks. The EOF is a really nice approach. I've used it before with ftp commands but I didn't strike the connection that I can use it in curl too.
â Bernie Lenz
Jan 3 at 23:00
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
There are multiple ways to escape long strings with different quotes. The simplest is to end the single quote and escape the single quote:
'...'''...'
But there are some nicer things you can do. Heredocs are a good way to avoid the quoting issue:
curl -s -X POST https://www.hashemian.com/tools/form-post-tester.php
-H "Content-Type: application/json"
-H "Accept: application/json"
-d @- <<EOF
"Variable1":"Value1",
"Variable2":"Value2's",
"Variable3":"Value3",
"Variable4":"$Value4Variable",
"Variable5":"Value5"
EOF
@-
tell curl to read from stdin and <<EOF
starts the heredoc which will be feed into the stdin of curl. The nice thing with the heredoc is that you do not need to escape any quotes and can use bash variables inside them sidestepping the need to worry about how to escape the quotes and making the whole thing much more readable.
There are multiple ways to escape long strings with different quotes. The simplest is to end the single quote and escape the single quote:
'...'''...'
But there are some nicer things you can do. Heredocs are a good way to avoid the quoting issue:
curl -s -X POST https://www.hashemian.com/tools/form-post-tester.php
-H "Content-Type: application/json"
-H "Accept: application/json"
-d @- <<EOF
"Variable1":"Value1",
"Variable2":"Value2's",
"Variable3":"Value3",
"Variable4":"$Value4Variable",
"Variable5":"Value5"
EOF
@-
tell curl to read from stdin and <<EOF
starts the heredoc which will be feed into the stdin of curl. The nice thing with the heredoc is that you do not need to escape any quotes and can use bash variables inside them sidestepping the need to worry about how to escape the quotes and making the whole thing much more readable.
answered Jan 3 at 18:45
Michael Daffin
2,5801517
2,5801517
Thanks. The EOF is a really nice approach. I've used it before with ftp commands but I didn't strike the connection that I can use it in curl too.
â Bernie Lenz
Jan 3 at 23:00
add a comment |Â
Thanks. The EOF is a really nice approach. I've used it before with ftp commands but I didn't strike the connection that I can use it in curl too.
â Bernie Lenz
Jan 3 at 23:00
Thanks. The EOF is a really nice approach. I've used it before with ftp commands but I didn't strike the connection that I can use it in curl too.
â Bernie Lenz
Jan 3 at 23:00
Thanks. The EOF is a really nice approach. I've used it before with ftp commands but I didn't strike the connection that I can use it in curl too.
â Bernie Lenz
Jan 3 at 23:00
add a comment |Â
up vote
1
down vote
I would use --data @filename
notation to read the data from file:
Sample json-data-file
contents:
"Variable1":"Value1",
"Variable2":"Value2's",
"Variable3":"Value3",
"Variable4":"'$Value4Variable'",
"Variable5":"Value5"
The request:
Value4Variable="Value4"
curl -X POST -H "Content-Type: application/json" -H "Accept: application/json"
-d @<(sed "s/$Value4Variable/$Value4Variable/" json-data-file)
https://www.hashemian.com/tools/form-post-tester.php/test123
The output:
The POSTed value is:
********************
"Variable1":"Value1", "Variable2":"Value2's","Variable3":"Value3","Variable4":"'Value4'", "Variable5":"Value5"
********************
Results saved to: test123
*Note: Results are purged periodically. 1 hr minimum life.
Thanks! This works although I prefer to have it all in one file as there are multiple curls in the script and splitting them all into different files makes maintenance a bit more convoluted.
â Bernie Lenz
Jan 3 at 22:57
add a comment |Â
up vote
1
down vote
I would use --data @filename
notation to read the data from file:
Sample json-data-file
contents:
"Variable1":"Value1",
"Variable2":"Value2's",
"Variable3":"Value3",
"Variable4":"'$Value4Variable'",
"Variable5":"Value5"
The request:
Value4Variable="Value4"
curl -X POST -H "Content-Type: application/json" -H "Accept: application/json"
-d @<(sed "s/$Value4Variable/$Value4Variable/" json-data-file)
https://www.hashemian.com/tools/form-post-tester.php/test123
The output:
The POSTed value is:
********************
"Variable1":"Value1", "Variable2":"Value2's","Variable3":"Value3","Variable4":"'Value4'", "Variable5":"Value5"
********************
Results saved to: test123
*Note: Results are purged periodically. 1 hr minimum life.
Thanks! This works although I prefer to have it all in one file as there are multiple curls in the script and splitting them all into different files makes maintenance a bit more convoluted.
â Bernie Lenz
Jan 3 at 22:57
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I would use --data @filename
notation to read the data from file:
Sample json-data-file
contents:
"Variable1":"Value1",
"Variable2":"Value2's",
"Variable3":"Value3",
"Variable4":"'$Value4Variable'",
"Variable5":"Value5"
The request:
Value4Variable="Value4"
curl -X POST -H "Content-Type: application/json" -H "Accept: application/json"
-d @<(sed "s/$Value4Variable/$Value4Variable/" json-data-file)
https://www.hashemian.com/tools/form-post-tester.php/test123
The output:
The POSTed value is:
********************
"Variable1":"Value1", "Variable2":"Value2's","Variable3":"Value3","Variable4":"'Value4'", "Variable5":"Value5"
********************
Results saved to: test123
*Note: Results are purged periodically. 1 hr minimum life.
I would use --data @filename
notation to read the data from file:
Sample json-data-file
contents:
"Variable1":"Value1",
"Variable2":"Value2's",
"Variable3":"Value3",
"Variable4":"'$Value4Variable'",
"Variable5":"Value5"
The request:
Value4Variable="Value4"
curl -X POST -H "Content-Type: application/json" -H "Accept: application/json"
-d @<(sed "s/$Value4Variable/$Value4Variable/" json-data-file)
https://www.hashemian.com/tools/form-post-tester.php/test123
The output:
The POSTed value is:
********************
"Variable1":"Value1", "Variable2":"Value2's","Variable3":"Value3","Variable4":"'Value4'", "Variable5":"Value5"
********************
Results saved to: test123
*Note: Results are purged periodically. 1 hr minimum life.
edited Jan 3 at 18:46
answered Jan 3 at 18:45
RomanPerekhrest
22.4k12145
22.4k12145
Thanks! This works although I prefer to have it all in one file as there are multiple curls in the script and splitting them all into different files makes maintenance a bit more convoluted.
â Bernie Lenz
Jan 3 at 22:57
add a comment |Â
Thanks! This works although I prefer to have it all in one file as there are multiple curls in the script and splitting them all into different files makes maintenance a bit more convoluted.
â Bernie Lenz
Jan 3 at 22:57
Thanks! This works although I prefer to have it all in one file as there are multiple curls in the script and splitting them all into different files makes maintenance a bit more convoluted.
â Bernie Lenz
Jan 3 at 22:57
Thanks! This works although I prefer to have it all in one file as there are multiple curls in the script and splitting them all into different files makes maintenance a bit more convoluted.
â Bernie Lenz
Jan 3 at 22:57
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%2f414597%2fbash-escaping-quotes%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
2
The same way you used for expanding the variable. End the single-quotes, insert a single-quote (either quoted or escaped), then start the single-quoted string again. So
'....'''....'
or'....'"'"'....'
â ilkkachu
Jan 3 at 18:30
1
Why not put the JSON into a data file, and use
curl [...] --data @mydata.json
?â DopeGhoti
Jan 3 at 18:42
1
@DopeGhoti there is a variable that needs to be expanded inside the json - it won't be expanded inside a file without extra work.
â Michael Daffin
Jan 3 at 18:50