Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

10
  • Somehow, this question sat here for a couple of years, without an accurate answer. I just discovered the solution (answered separately here) and now I see you've posted almost the exact same answer only a few minutes before me. If only you'd posted this before, it would have saved me figuring it out on my own! Commented Jan 21, 2021 at 16:52
  • @jrw32982 I don't think I saw this question before, but Charles just edited their answer which bumped the question to the top of the home page, where I spotted it. Commented Jan 21, 2021 at 16:54
  • 1
    Yes, that's the only approach so far that handles arbitrary values. jq even happens to support non-text values in its strings as an extension over standard json. It seems to be using the safest form of quoting ('...'). I note that it transforms a NUL byte to \0 and doesn't quote numbers nor false/true though that should be fine. As usual, note that it may transform numbers (like change 1e2 to 100 or infinity to 1.7976931348623157e+308). Commented Jan 21, 2021 at 17:01
  • @StéphaneChazelas Do you think it's an issue that some values are left unquoted? If so, one could pass the data through tostring before letting @sh format them. Commented Jan 21, 2021 at 17:06
  • 1
    I don't expect it would here. There are context where not quoting numbers can be an issue (like in echo '2'>file vs echo 2>file), but no here I'd say. Commented Jan 21, 2021 at 17:10