Iokanaan Iokan
Iokanaan Iokan

Reputation: 775

How to pass a variable containing quotes and spaces as part of a single argument to a command

I have the following line in my bash script:

curl -i -X POST -H 'Content-Type: application/json-rpc' \
     -d '{"jsonrpc": "2.0", "method": "configuration.export", "params": { "options": { "templates": [ '$ftemplateids' ] }, "format": "xml"}, "auth": '$authtoken', "id": 1 }' \
     http://example.com/zabbix/api_jsonrpc.php |
grep jsonrpc | tail -c +28 | head -c -12 > file.xml

As you can see, there are two variables: $authtoken and $ftemplateids. I have no problems with $authtoken; it gets parsed without any problems. The problem is with $ftemplateids.

When I echo the variable, it shows as this, which is the desired format:

"10001", "10047", "10050", "10056", "10060", "10065", "10066", "10067", "10068", "10069", "10070", "10071", "10072", "10073", ......., "14433"

However, when I run the script, it outputs the following:

curl -i -X POST -H 'Content-Type: application/json-rpc' -d '{"jsonrpc": "2.0", "method": "configuration.export", "params": { "options": { "templates": [ "10001",' '"10047",' '"10050",' '"10056",' '"10060",' '"10065",' '"10066",' '"10067",' '"10068",' '"10069",' '"10070",' '"10071",' '"10072",' '"10073",' '"10074",' '"10075",' '"10076",' '"10077",' '"10078",' '"10079",' '"10081",' '"10082",' '"10083",' '"10086",' '"10089",' '"10592",' '"10595",' '"10599",' '"10726",' '"10737",' '"10739",' '"10758",' '"10763",' '"10764",' '"10769",' '"10776",' '"10809",' '"10810",' '"10876",' '"10880",' '"10881",' '"10882",' '"10921",' '"10924",' '"10961",' '"10966",' '"11005",' '"11006",' '"11007",' '"11008",' '"11010",' '"11011",' '"11012",' '"11035",' '"11036",' '"11037",' '"11038",' '"11120",' '"11126",' '"11146",' '"11147",' '"11148",' '"11149",' '"11151",' '"11167",' '"11168",' '"11169",' '"11170",' '"11264",' '"12256",' '"12264",' '"12281",' '"12315",' '"12418",' '"12420",' '"12780",' '"12788",' '"12799",' '"13771",' '"13772",' '"13776",' '"13781",' '"13790",' '"13791",' '"14433" ] }, "format": "xml"}, "auth": "authtokenwashere", "id": 1 }' http://example.com/zabbix/api_jsonrpc.php

I've tried [ "$ftemplateids" ] but that did not help.

Upvotes: 0

Views: 168

Answers (2)

D3Hunter
D3Hunter

Reputation: 1349

William Pursell gives the right way to solve your problem.

Some explanation of why this happens:

Because "The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting."

The shell treats each character of $IFS as a delimiter, and splits the results of the other expansions into words on these characters. If IFS is unset, it is <space><tab><newline> by default

In your command $ftemplateids was not enclosed with double quotes, and you have spaces in your variable, so your variable was split into multiple arguments.

Upvotes: 1

William Pursell
William Pursell

Reputation: 212198

Your instinct to use double quotes is correct, but you still need to terminate the single quoted string. Try:

… [ '"$ftemplateids"' ] …

Upvotes: 4

Related Questions