bash escaping quotes

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question
















  • 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














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.







share|improve this question
















  • 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












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.







share|improve this question












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.









share|improve this question











share|improve this question




share|improve this question










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 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












  • 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







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










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.






share|improve this answer




















  • 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

















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.





share|improve this answer






















  • 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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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.






share|improve this answer




















  • 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














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.






share|improve this answer




















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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












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.





share|improve this answer






















  • 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














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.





share|improve this answer






















  • 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












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.





share|improve this answer














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.






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?