Inside single-quotes, the shell expands nothing. Place them inside double-quotes instead:
curl -u <my-api-token>: \
-X POST https://api.pushbullet.com/v2/pushes \
--header 'Content-Type: application/json' \
--data-binary '{"type": "note", "title": "'"$TR_TORRENT_NAME"'", \
"body": "'"$TR_TORRENT_NAME completed"'."}'
Let's examine how this works by looking at:
$ TR_TORRENT_NAME=MyTorrent
$ echo '{"type": "note", "title": "'"$TR_TORRENT_NAME"'", "body": "'"$TR_TORRENT_NAME completed"'."}'
{"type": "note", "title": "MyTorrent", "body": "MyTorrent completed."}
When the shell variable appears, it is always inside double-quotes. Consequently, it is properly expanded.
Quoting like this is a bit subtle. We have single-quoted strings that contain double-quotes as characters and are next to double-quoted strings. To understand this better, let's take this fragment as a an example:
"'"$TR_TORRENT_NAME"'"
Taking each character in turn:
"
is a literal double-quote character that is inside of a single-quoted string. (For brevity, the beginning of this string is not shown in this fragment.)
'
closes a single-quoted string.
"
opens a double-quoted string.
$TR_TORRENT_NAME
is a shell variable that is expanded inside double-quotes.
"
closes the double-quoted string.
'
opens a new single-quoted string.
"
places a double-quote character inside the single-quoted string.
Do you need steps 3 and 5? – davidfrancis – 2018-01-18T11:39:28.500
@davidfrancis If one omits steps 3 and 5, then step 4 is subject to word splitting and pathname expansion and either one could cause all manor of trouble. Unless one explicitly wants word splitting and pathname expansion, a shell variable should always be inside double-quotes. – John1024 – 2018-01-18T20:49:11.160
Thanks for that, can you give a quick example please? It worked in my own example, which is why I asked, but there were no spaces or anything else complex in there – davidfrancis – 2018-01-19T09:10:18.937
@davidfrancis Try
TR_TORRENT_NAME="A * B"
and see what happens. – John1024 – 2018-01-20T08:17:14.407