0

I am trying to extract value from a JSON file using jq on Ubuntu. When I echo out directly, it works and returns the value to the console. But when I try to assign the extracted value to a variable and echo out, returns nothing to console.

The following works:

#!/bin/sh
echo '{"USERNAME":"TRX-101","PASSWORD":":g&fg#/H"}' | jq .PASSWORD

The following does NOT work:

#!/bin/sh
VAR = '{"USERNAME":"TRX-101","PASSWORD":":g&fg#/H"}' | jq .PASSWORD
echo $VAR

My ultimate goal is to use the value from $VAR to do other operations within the shell script (ex: send the value to a XML file etc.)

Inian
  • 80,270
  • 14
  • 142
  • 161
cloudify
  • 95
  • 4
  • 10
  • 2
    Nothing jq-specific to this whatsoever -- `VAR = ls`, for example, doesn't work either. Consider making a habit of running your code through http://shellcheck.net/ before asking questions here -- and fix *everything* it finds; not quoting the argument to `echo`, for example, [can absolutely cause bugs](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo), even if they don't show up with simple values being tested. – Charles Duffy Jan 28 '19 at 17:49
  • 1
    Also, `/bin/sh` is `sh`, not `bash`; please tag for the shell you're actually using (and if you *want* to use bash, use `#!/bin/bash` or `#!/usr/bin/env bash` as your shebang). – Charles Duffy Jan 28 '19 at 17:52
  • 1
    ...btw, the specific shellcheck warnings for your bugs are [SC2036](https://github.com/koalaman/shellcheck/wiki/SC2036) (assigning pipeline output to variables), and [SC1068](https://github.com/koalaman/shellcheck/wiki/SC1068) (using spaces around `=` where they aren't allowed). – Charles Duffy Jan 28 '19 at 17:54

0 Answers0