Reputation: 41
I'm trying to set up a .sh script which will send a POST command which includes a variable.
I've surrounded the quote with double quotes, and tried different combinations of double and single quotes around the variable. I've also tried making the whole string into three variables and concatenating them but none of them work properly (either the server gives errors or nothing happens when it should)
The code which I mostly got straight from Postman
playlist="Ambient.m3u"
curl -X POST "http://192.168.1.96:32400/playlists/upload?sectionID=11&path=D:\Media\Plex%20Playlists\${playlist}&X-Plex-Token={REMOVED}" -H 'Postman-Token: {REMOVED}' -H 'cache-control: no-cache'
When I replace
${playlist}
with
Ambient.m3u
the command works (Plex adds the playlist), but I can't get it working with a variable.
Upvotes: 1
Views: 405
Reputation: 4455
Backslashes will escape the $ in your "double quotes". In order to pass a backslash, you can escape the backslash with a backslash ( of course! ).
Your command becomes:
playlist="Ambient.m3u"
curl -X POST "http://192.168.1.96:32400/playlists/upload?sectionID=11&path=D:\\Media\\Plex%20Playlists\\${playlist}&X-Plex-Token={REMOVED}" -H 'Postman-Token: {REMOVED}' -H 'cache-control: no-cache'
As it turns out, bash seems smart enough to not escape the letters in your path. That is, "\M" is the same as "\M" ( try it: echo "\M" vs. echo "\M" ). So, it is fine not to put all those \ in there, but you definitely need the \ in front of the $ to avoid escaping the $. This explains why your expression works when you put a constant for the file name:
This works (you only need to escape the \ in front of $):
curl -X POST "http://192.168.1.96:32400/playlists/upload?sectionID=11&path=D:\Media\Plex%20Playlists\\${playlist}&X-Plex-Token={REMOVED}" -H 'Postman-Token: {REMOVED}' -H 'cache-control: no-cache'
Upvotes: 1