I have a shell script with 3 Parameters:
#!/bin/bash
VHOST=$1
EXCHANGE=$2
DELAY=$3
RABBIT_CMD="docker exec rabbit rabbitmqadmin -u rabbit -p rabbit --vhost=$VHOST"
COMPLEX_CMD="$RABBIT_CMD declare queue name=${EXCHANGE}-delay"
COMPLEX_CMD="$COMPLEX_CMD arguments="
COMPLEX_CMD="${COMPLEX_CMD}'{\"x-message-ttl\":$DELAY,\"x-dead-letter-exchange\":\"$EXCHANGE\", \"x-dead-letter-routing-key\":\"worker\"}'"
echo $COMPLEX_CMD
$COMPLEX_CMD
Now I call this script
./script.sh rdb blah 5000
The second last line echo $COMPLEX_CMD
outputs the following line:
docker exec rabbit rabbitmqadmin -u rabbit -p rabbit --vhost=rdb declare queue name=blah-delay arguments='{"x-message-ttl":5000,"x-dead-letter-exchange":"blah","x-dead-letter-routing-key":"worker"}'
When I copy-paste this into my bash and execute it, it works without any problems. But when I want to execute this in the script (last line $COMPLEX_CMD
), I get the following error:
ERROR: Could not parse JSON:
'{"x-message-ttl":5000,"x-dead-letter-exchange":"blah","x-dead-letter-routing-key":"worker"}'
How do I have to escape my strings within the JSON in the right manner?