Mosquitto mqtt example without escaping quotes?

This is a not exactly a question about node red, but since this community is also expert in related fields, perhaps someone can answer anyway.

I've been playing around with mosquitto_pub and an example from thingsboard:
MQTT Device API Reference | ThingsBoard Community Edition

mosquitto_pub -d -h "demo.thingsboard.io" -t "v1/devices/me/attributes" -u "$ACCESS_TOKEN" -m "{"attribute1": "value1", "attribute2": true}"

What I don't understand is how un-escaped quotes works in the message (-m flag). It doesn't escape the quotes inside json, nor does it encapsulate with single quotes. Yet this works directly from linux terminal? Chatgpt insists this is not possible, and either the copied text is not the same (something I can't observe, because pasting it in notepad shows it as identical), or that the terminal somehow magically escapes it when pasting (also not the case)?

Perhaps mosquitto is being nice and automatically handles this? But that's kinda unusual behavior for cli programs? Chatgpt also denies this...

Indeed, very odd. At face value, it shouldn't work. I think this gets a classification of "magic". :wink:

1 Like

Are you you sure it works? For me it nearly works, but the string I get back is
{attribute1: value1, attribute2: true}
which is not valid JSON, all the quotes are missing. How it works at all I don't know. It does work if you use single quotes round the value.
-m '{"attribute1": "value1", "attribute2": true}'

1 Like

All of these quotes and braces get a thorough going-over by Bash before they arrive at mosquitto_pub, and what comes out of the wringer does as Colin says, sort of work.
I think it's related to how Bash interprets squiggly brackets, though it seems to work in Dash too.

You should definitely protect the quotes in your json by wrapping the message inside single quotes.