`jq -r '.[] | select(.name == $svc) | .port' services.json` fails with `jq: error: $svc is not defined at <top-level>, line 1:`.
IntermediateTroubleshootingTroubleshooting: tools, portability, permissions and environment
Answer
Likely causes → Inside single quotes the shell does not expand $svc, so jq sees a jq variable that was never defined. The tempting "fix", switching to double quotes, splices raw shell text into the jq program, which breaks on quotes in the value and allows filter injection.
Diagnose → The error names the undefined jq variable; echo "$svc" confirms the shell has it.
Fix → Pass shell values as jq variables:
jq -r --arg svc "$svc" '.[] | select(.name == $svc) | .port' services.json
jq -n --arg host "$host" --argjson port "$port" '{host: $host, port: $port}' # building JSON safely
Prevent → Never build jq filters (or JSON bodies) by string interpolation; use --arg, --argjson and --slurpfile.
More on Troubleshooting: tools, portability, permissions and environment
On a Mac, `sed -i 's/replicas: 2/replicas: 3/' deploy.yaml` fails with `sed: 1: "deploy.yaml": extra characters at the end of d command`.BeginnerA cleanup script fails on a developer's Mac with `date: illegal option -- d`.BeginnerA script that works on Ubuntu fails in an Alpine-based CI image with `grep: unrecognized option: P`.IntermediateAn `awk` one-liner works on your RHEL box but fails on Ubuntu with `awk: line 1: function gensub never defined`.Intermediate`comm -23 expected.txt installed.txt` prints `comm: file 1 is not in sorted order`, even though both files were produced with `sort`.Intermediate`grep ERROR /var/log/app/app.log` prints only `grep: /var/log/app/app.log: binary file matches`.Beginner