Skip to content

jq: support --arg and --slurpfile variable bindings #317

Description

@chubes4

Summary

The built-in jq command does not recognize the standard --arg or --slurpfile options in just-bash@3.1.0. These options are needed to bind shell values and JSON files into jq filters without interpolating untrusted values into the filter source.

Reproduction

--arg

echo '["Alice", "Bob"]' | jq --arg name Alice '[.[] | select(. == $name)]'

Actual result:

jq: unrecognized option '--arg'

Expected result:

[
  "Alice"
]

--slurpfile

printf '["stdout line"]\n' > /tmp/stdout.json
printf '[]\n' > /tmp/log.json
jq --slurpfile stdout /tmp/stdout.json '. += [{stdout: $stdout[0]}]' /tmp/log.json

Actual result:

jq: unrecognized option '--slurpfile'

Expected result:

[
  {
    "stdout": [
      "stdout line"
    ]
  }
]

Use case

Shell scripts commonly use these bindings to construct structured command logs safely:

jq \
  --arg command "$command" \
  --arg exitCode "$exitCode" \
  --slurpfile stdout "$stdoutJson" \
  '. += [{command: $command, exitCode: ($exitCode | tonumber), stdout: $stdout[0]}]' \
  "$logFile"

Without variable bindings, callers must rewrite valid jq scripts or interpolate shell values into jq source, which is fragile and can introduce injection bugs.

Expected behavior

  • --arg name value binds value as a string variable.
  • --slurpfile name file parses all JSON values in file and binds them as an array.
  • Multiple bindings can be combined in one invocation.
  • The behavior works with the browser/VFS implementation as well as the Node entry point.
  • Invalid JSON and missing option arguments return jq-compatible nonzero statuses and diagnostics.

Prior report

This overlaps #175, which reported --arg against 2.14.0. That issue was closed by its author without maintainer discussion or a linked implementation. The behavior remains reproducible in 3.1.0, and --slurpfile was not covered there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions