Three ways a jq one-liner lies to your bash script

●1 ●23 ●87
calendar_today • schedule1 min read

Most "the filter looks right but the script misbehaves" bugs come down to three silent failure modes.

First, a missing -r. jq outputs JSON by default, so a string arrives with its quotes attached — your variable holds "web-01", quotes included, and a comparison against web-01 never matches. Any value headed into a shell variable, filename, or test wants -r.

Second, missing keys don't error. jq emits null, which lands in bash as a literal four-character string — non-empty, so [ -n ] guards pass and backup-null.tar.gz gets written. The // operator supplies a real fallback.

Third, select() compares exactly. A value that is almost right matches nothing, and nothing is not an error:

jq -r '.items[] | select(.active == true) | .name'

If the API sends "active": "true" as a string, that item is skipped. If you match "Web-01" against "web-01", zero elements come back. Either way jq prints nothing and exits 0, so the silence reads as success.

All three share the same fingerprint: exit code zero, plausible output, wrong result. The fastest way I've found to catch them before they ship is building the filter against the actual response: this interactive jq filter builder with a live in-browser preview lets you click through your real JSON to build the path, handles the -r, the // fallback and select() quoting for you, and shows exactly what the filter emits before anything runs.

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

My 30-day log cleanup kept a file that was 30 and a half days old

BashSnippets - Oct 8

A Textile Mill's Dyehouse Ramp Curve: Get One Step Wrong and the Sheet Belongs to the Master

aiomniu - Oct 18

I escaped the dot correctly and sed still rewrote a file marked DO NOT CHANGE

BashSnippets - Oct 13

The log was deleted. Its 48M was still allocated.

BashSnippets - Oct 28

I killed the process I started. The port stayed taken.

BashSnippets - Nov 5
chevron_left
4k Points • 111 Badges
North America • bashsnippets.xyz
77Posts
35Comments
10Connections
Linux user who got tired of Googling the same bash commands every time I sat down at a terminal. Sta... Show more

Related Jobs

Commenters (This Week)

3 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!