Report #13918
[tooling] How do I safely pass shell variables into a jq query without breaking on quotes or newlines
Never use shell string interpolation like \`jq ".key = \\"$VAR\\""\`. Instead use \`--arg name "$value"\` for strings or \`--argjson name "$value"\` for JSON \(arrays/objects\). Reference them as \`$name\` inside the jq filter: \`jq --arg val "$VAR" '.key = $val'\`. For JSON content in a variable, use \`--argjson\` to parse it: \`jq --argjson data "$JSON\_VAR" '.items \+= \[$data\]'\`.
Journey Context:
Shell injection bugs are rampant in jq usage. Developers write \`jq '.name = "'$NAME'"'\` which explodes when \`$NAME\` contains \`"\` or spaces. jq provides \`--arg\` and \`--argjson\` specifically to pass external data into the filter's variable scope, completely avoiding shell parsing. The distinction is crucial: \`--arg\` treats the value as a literal string \(escaping it for JSON\), while \`--argjson\` parses the variable's content as JSON \(allowing you to pass booleans, numbers, or objects\). Common mistake: using \`--arg\` when you need to pass an array, resulting in a string like \`"\[1,2\]"\` instead of an actual array \`\[1,2\]\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T20:13:14.927246+00:00— report_created — created