How to execute a command that has many nested single and double quotes with a quoted variable in bashIs there...

Does this circuit have marginal voltage level problem?

What do you call the angle of the direction of an airplane?

Go function to test whether a file exists

how to set the columns in pandas

SQL Server error 242 with ANSI datetime

Why would a propellor have blades of different lengths?

Which high-degree derivatives play an essential role?

Why did moving the mouse cursor cause Windows 95 to run more quickly?

A grammar issue?

Auto replacement of characters

Who pays for increased security measures on flights to the US?

Performance of loop vs expansion

What is the difference between figures illustration and images?

Construction of the word подтвержда́ть

Cannot update a field to a Lookup, MasterDetail, or Hierarchy from something else (44:13)

When do I make my first save against the Web spell?

Where is read command?

What is the right way to query an I2C device from an interrupt service routine?

Should I hide my travel history to the UK when I apply for an Australian visa?

Why is quantum gravity non-renormalizable?

Isn't "Dave's protocol" good if only the database, and not the code, is leaked?

How long had Bertha Mason been in the attic at the point of the events in Jane Eyre

My players like to search everything. What do they find?

What can a novel do that film and TV cannot?



How to execute a command that has many nested single and double quotes with a quoted variable in bash


Is there any way to print value inside variable inside single quote?Bash Script : Passing a variable to a bash script that contains quotes, single quotes. etcSingle quote within double quotes and the Bash reference manualWhat is the difference between single quoted $'string' and double quoted $“string” in bash?bash - Single quotes being added to double quotesUsing a variable to execute a curl commandHow to compare bash variable with double quotesPassing an argument with double and single quotes to another call in bashbash variable with quotes and percentageExecute command from string with both single and double quotesBash: Escaping double quotes in $() command substitution






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















Here is my simple script



#!/bin/sh
thefile=/home/ark/arkserver/ShooterGame/Saved/SaveIsland/1288804998.arktribe

while inotifywait "${thefile}" ; do
a="`strings ${thefile} | tail -n 5 | head -n 1 | sed 's/<[^>]*>//g'`"
echo $a

curl -H "Content-Type: application/json" -X POST -d '{"username": "Island", "content": $a}' https://discordapp.com/api/webhooks/5738674701/OjRQiAWHq5mX0Tn2MfBlF-mI41TWrVYVAbOfXpeZWqo8

done


Here is the output



Setting up watches.
Watches established.
/home/ark/arkserver/ShooterGame/Saved/SaveIsland/1288804998.arktribe ATTRIB
"Day 600, 21:31:17: Phorce1 demolished a 'Campfire'!"
{"code": 50006, "message": "Cannot send an empty message"}Setting up watches.
Watches established.


I cannot figure out how to get the curl command properly quoted with the variable inserted in the middle. This is what the command should look like. This works when typed out on the command line



curl -H "Content-Type: application/json" -X POST -d '{"username": "test", "content": "Day 600, 14:51:00: Phorce1 demolished a 'Campfire'!"}' https://discordapp.com/api/webhooks/594723628738674701/OjRQn2MfBlF-FJVl1cWwMlD6UQf0c


I have tried many different forms of quoting. The fact that it echos $a as properly quoted but still breaks something in the command that uses it confuses me.



(Yes, I mangled my webhook address)










share|improve this question

























  • Possible duplicate of Is there any way to print value inside variable inside single quote?

    – rush
    13 hours ago











  • Bash doesn't expand variable in single quotes. You have either use " instead, either put additional ' around your variable.

    – rush
    13 hours ago











  • @rush Please note the answer I selected here is quite different from the replies to that question. I tried escaping the quotes around the variable but the "many nested quotes" nature of he curl command line cause that to fail. So, no, this is not a duplicate of that question.

    – Phorce1
    5 hours ago


















0















Here is my simple script



#!/bin/sh
thefile=/home/ark/arkserver/ShooterGame/Saved/SaveIsland/1288804998.arktribe

while inotifywait "${thefile}" ; do
a="`strings ${thefile} | tail -n 5 | head -n 1 | sed 's/<[^>]*>//g'`"
echo $a

curl -H "Content-Type: application/json" -X POST -d '{"username": "Island", "content": $a}' https://discordapp.com/api/webhooks/5738674701/OjRQiAWHq5mX0Tn2MfBlF-mI41TWrVYVAbOfXpeZWqo8

done


Here is the output



Setting up watches.
Watches established.
/home/ark/arkserver/ShooterGame/Saved/SaveIsland/1288804998.arktribe ATTRIB
"Day 600, 21:31:17: Phorce1 demolished a 'Campfire'!"
{"code": 50006, "message": "Cannot send an empty message"}Setting up watches.
Watches established.


I cannot figure out how to get the curl command properly quoted with the variable inserted in the middle. This is what the command should look like. This works when typed out on the command line



curl -H "Content-Type: application/json" -X POST -d '{"username": "test", "content": "Day 600, 14:51:00: Phorce1 demolished a 'Campfire'!"}' https://discordapp.com/api/webhooks/594723628738674701/OjRQn2MfBlF-FJVl1cWwMlD6UQf0c


I have tried many different forms of quoting. The fact that it echos $a as properly quoted but still breaks something in the command that uses it confuses me.



(Yes, I mangled my webhook address)










share|improve this question

























  • Possible duplicate of Is there any way to print value inside variable inside single quote?

    – rush
    13 hours ago











  • Bash doesn't expand variable in single quotes. You have either use " instead, either put additional ' around your variable.

    – rush
    13 hours ago











  • @rush Please note the answer I selected here is quite different from the replies to that question. I tried escaping the quotes around the variable but the "many nested quotes" nature of he curl command line cause that to fail. So, no, this is not a duplicate of that question.

    – Phorce1
    5 hours ago














0












0








0








Here is my simple script



#!/bin/sh
thefile=/home/ark/arkserver/ShooterGame/Saved/SaveIsland/1288804998.arktribe

while inotifywait "${thefile}" ; do
a="`strings ${thefile} | tail -n 5 | head -n 1 | sed 's/<[^>]*>//g'`"
echo $a

curl -H "Content-Type: application/json" -X POST -d '{"username": "Island", "content": $a}' https://discordapp.com/api/webhooks/5738674701/OjRQiAWHq5mX0Tn2MfBlF-mI41TWrVYVAbOfXpeZWqo8

done


Here is the output



Setting up watches.
Watches established.
/home/ark/arkserver/ShooterGame/Saved/SaveIsland/1288804998.arktribe ATTRIB
"Day 600, 21:31:17: Phorce1 demolished a 'Campfire'!"
{"code": 50006, "message": "Cannot send an empty message"}Setting up watches.
Watches established.


I cannot figure out how to get the curl command properly quoted with the variable inserted in the middle. This is what the command should look like. This works when typed out on the command line



curl -H "Content-Type: application/json" -X POST -d '{"username": "test", "content": "Day 600, 14:51:00: Phorce1 demolished a 'Campfire'!"}' https://discordapp.com/api/webhooks/594723628738674701/OjRQn2MfBlF-FJVl1cWwMlD6UQf0c


I have tried many different forms of quoting. The fact that it echos $a as properly quoted but still breaks something in the command that uses it confuses me.



(Yes, I mangled my webhook address)










share|improve this question
















Here is my simple script



#!/bin/sh
thefile=/home/ark/arkserver/ShooterGame/Saved/SaveIsland/1288804998.arktribe

while inotifywait "${thefile}" ; do
a="`strings ${thefile} | tail -n 5 | head -n 1 | sed 's/<[^>]*>//g'`"
echo $a

curl -H "Content-Type: application/json" -X POST -d '{"username": "Island", "content": $a}' https://discordapp.com/api/webhooks/5738674701/OjRQiAWHq5mX0Tn2MfBlF-mI41TWrVYVAbOfXpeZWqo8

done


Here is the output



Setting up watches.
Watches established.
/home/ark/arkserver/ShooterGame/Saved/SaveIsland/1288804998.arktribe ATTRIB
"Day 600, 21:31:17: Phorce1 demolished a 'Campfire'!"
{"code": 50006, "message": "Cannot send an empty message"}Setting up watches.
Watches established.


I cannot figure out how to get the curl command properly quoted with the variable inserted in the middle. This is what the command should look like. This works when typed out on the command line



curl -H "Content-Type: application/json" -X POST -d '{"username": "test", "content": "Day 600, 14:51:00: Phorce1 demolished a 'Campfire'!"}' https://discordapp.com/api/webhooks/594723628738674701/OjRQn2MfBlF-FJVl1cWwMlD6UQf0c


I have tried many different forms of quoting. The fact that it echos $a as properly quoted but still breaks something in the command that uses it confuses me.



(Yes, I mangled my webhook address)







bash variable-substitution






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago







Phorce1

















asked 13 hours ago









Phorce1Phorce1

237 bronze badges




237 bronze badges













  • Possible duplicate of Is there any way to print value inside variable inside single quote?

    – rush
    13 hours ago











  • Bash doesn't expand variable in single quotes. You have either use " instead, either put additional ' around your variable.

    – rush
    13 hours ago











  • @rush Please note the answer I selected here is quite different from the replies to that question. I tried escaping the quotes around the variable but the "many nested quotes" nature of he curl command line cause that to fail. So, no, this is not a duplicate of that question.

    – Phorce1
    5 hours ago



















  • Possible duplicate of Is there any way to print value inside variable inside single quote?

    – rush
    13 hours ago











  • Bash doesn't expand variable in single quotes. You have either use " instead, either put additional ' around your variable.

    – rush
    13 hours ago











  • @rush Please note the answer I selected here is quite different from the replies to that question. I tried escaping the quotes around the variable but the "many nested quotes" nature of he curl command line cause that to fail. So, no, this is not a duplicate of that question.

    – Phorce1
    5 hours ago

















Possible duplicate of Is there any way to print value inside variable inside single quote?

– rush
13 hours ago





Possible duplicate of Is there any way to print value inside variable inside single quote?

– rush
13 hours ago













Bash doesn't expand variable in single quotes. You have either use " instead, either put additional ' around your variable.

– rush
13 hours ago





Bash doesn't expand variable in single quotes. You have either use " instead, either put additional ' around your variable.

– rush
13 hours ago













@rush Please note the answer I selected here is quite different from the replies to that question. I tried escaping the quotes around the variable but the "many nested quotes" nature of he curl command line cause that to fail. So, no, this is not a duplicate of that question.

– Phorce1
5 hours ago





@rush Please note the answer I selected here is quite different from the replies to that question. I tried escaping the quotes around the variable but the "many nested quotes" nature of he curl command line cause that to fail. So, no, this is not a duplicate of that question.

– Phorce1
5 hours ago










1 Answer
1






active

oldest

votes


















2














Single quotes prevent variable expansion.



I would do this:



url=https://discordapp.com/api/webhooks/5738674701/OjRQiAWHq5mX0Tn2MfBlF-mI41TWrVYVAbOfXpeZWqo8
while inotifywait "$thefile" ; do
a=$(strings "$thefile" | tail -n 5 | head -n 1 | sed 's/<[^>]*>//g')
echo "$a"
payload=$(printf '{"username": "Island", "content": "%s"}' "$a")
curl -H "Content-Type: application/json" -X POST -d "$payload" "$url"
done


You don't need to escape double quotes inside of single quotes. In fact, if you do, the escape will be preserved as a literal character.



Always quote your variables, unless you know exactly what might happen if you don't.



(This is a matter of personal style) You don't need to brace a variable inside of quotes, unless you need to disambiguate the variable name from surrounding text:



echo "$aVariable"
echo "${aVariable}_abc"





share|improve this answer
























  • Thank you. I write little scripts so seldom I had to go look up a refresher on printf to figure out what the %s was doing. I was also looking at other similar questions that suggested creating a function for the curl command but this, fixing my quoting using variables, feels more right to me.

    – Phorce1
    13 hours ago
















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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
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%2f527896%2fhow-to-execute-a-command-that-has-many-nested-single-and-double-quotes-with-a-qu%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Single quotes prevent variable expansion.



I would do this:



url=https://discordapp.com/api/webhooks/5738674701/OjRQiAWHq5mX0Tn2MfBlF-mI41TWrVYVAbOfXpeZWqo8
while inotifywait "$thefile" ; do
a=$(strings "$thefile" | tail -n 5 | head -n 1 | sed 's/<[^>]*>//g')
echo "$a"
payload=$(printf '{"username": "Island", "content": "%s"}' "$a")
curl -H "Content-Type: application/json" -X POST -d "$payload" "$url"
done


You don't need to escape double quotes inside of single quotes. In fact, if you do, the escape will be preserved as a literal character.



Always quote your variables, unless you know exactly what might happen if you don't.



(This is a matter of personal style) You don't need to brace a variable inside of quotes, unless you need to disambiguate the variable name from surrounding text:



echo "$aVariable"
echo "${aVariable}_abc"





share|improve this answer
























  • Thank you. I write little scripts so seldom I had to go look up a refresher on printf to figure out what the %s was doing. I was also looking at other similar questions that suggested creating a function for the curl command but this, fixing my quoting using variables, feels more right to me.

    – Phorce1
    13 hours ago


















2














Single quotes prevent variable expansion.



I would do this:



url=https://discordapp.com/api/webhooks/5738674701/OjRQiAWHq5mX0Tn2MfBlF-mI41TWrVYVAbOfXpeZWqo8
while inotifywait "$thefile" ; do
a=$(strings "$thefile" | tail -n 5 | head -n 1 | sed 's/<[^>]*>//g')
echo "$a"
payload=$(printf '{"username": "Island", "content": "%s"}' "$a")
curl -H "Content-Type: application/json" -X POST -d "$payload" "$url"
done


You don't need to escape double quotes inside of single quotes. In fact, if you do, the escape will be preserved as a literal character.



Always quote your variables, unless you know exactly what might happen if you don't.



(This is a matter of personal style) You don't need to brace a variable inside of quotes, unless you need to disambiguate the variable name from surrounding text:



echo "$aVariable"
echo "${aVariable}_abc"





share|improve this answer
























  • Thank you. I write little scripts so seldom I had to go look up a refresher on printf to figure out what the %s was doing. I was also looking at other similar questions that suggested creating a function for the curl command but this, fixing my quoting using variables, feels more right to me.

    – Phorce1
    13 hours ago
















2












2








2







Single quotes prevent variable expansion.



I would do this:



url=https://discordapp.com/api/webhooks/5738674701/OjRQiAWHq5mX0Tn2MfBlF-mI41TWrVYVAbOfXpeZWqo8
while inotifywait "$thefile" ; do
a=$(strings "$thefile" | tail -n 5 | head -n 1 | sed 's/<[^>]*>//g')
echo "$a"
payload=$(printf '{"username": "Island", "content": "%s"}' "$a")
curl -H "Content-Type: application/json" -X POST -d "$payload" "$url"
done


You don't need to escape double quotes inside of single quotes. In fact, if you do, the escape will be preserved as a literal character.



Always quote your variables, unless you know exactly what might happen if you don't.



(This is a matter of personal style) You don't need to brace a variable inside of quotes, unless you need to disambiguate the variable name from surrounding text:



echo "$aVariable"
echo "${aVariable}_abc"





share|improve this answer













Single quotes prevent variable expansion.



I would do this:



url=https://discordapp.com/api/webhooks/5738674701/OjRQiAWHq5mX0Tn2MfBlF-mI41TWrVYVAbOfXpeZWqo8
while inotifywait "$thefile" ; do
a=$(strings "$thefile" | tail -n 5 | head -n 1 | sed 's/<[^>]*>//g')
echo "$a"
payload=$(printf '{"username": "Island", "content": "%s"}' "$a")
curl -H "Content-Type: application/json" -X POST -d "$payload" "$url"
done


You don't need to escape double quotes inside of single quotes. In fact, if you do, the escape will be preserved as a literal character.



Always quote your variables, unless you know exactly what might happen if you don't.



(This is a matter of personal style) You don't need to brace a variable inside of quotes, unless you need to disambiguate the variable name from surrounding text:



echo "$aVariable"
echo "${aVariable}_abc"






share|improve this answer












share|improve this answer



share|improve this answer










answered 13 hours ago









glenn jackmanglenn jackman

54.7k6 gold badges76 silver badges115 bronze badges




54.7k6 gold badges76 silver badges115 bronze badges













  • Thank you. I write little scripts so seldom I had to go look up a refresher on printf to figure out what the %s was doing. I was also looking at other similar questions that suggested creating a function for the curl command but this, fixing my quoting using variables, feels more right to me.

    – Phorce1
    13 hours ago





















  • Thank you. I write little scripts so seldom I had to go look up a refresher on printf to figure out what the %s was doing. I was also looking at other similar questions that suggested creating a function for the curl command but this, fixing my quoting using variables, feels more right to me.

    – Phorce1
    13 hours ago



















Thank you. I write little scripts so seldom I had to go look up a refresher on printf to figure out what the %s was doing. I was also looking at other similar questions that suggested creating a function for the curl command but this, fixing my quoting using variables, feels more right to me.

– Phorce1
13 hours ago







Thank you. I write little scripts so seldom I had to go look up a refresher on printf to figure out what the %s was doing. I was also looking at other similar questions that suggested creating a function for the curl command but this, fixing my quoting using variables, feels more right to me.

– Phorce1
13 hours ago




















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f527896%2fhow-to-execute-a-command-that-has-many-nested-single-and-double-quotes-with-a-qu%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...