Im using ubuntu to store JSON-encoded text into a environment variable like this:
~ test='{"test": { "mmm":"432"}}'
~ echo $test
{"test": { "mmm":"432"}}
how can i access the key "test"
to print {"mmm":"432"}
?
something like
~ test='{"test": { "mmm":"432"}}'
~ echo $test.test
{ "mmm":"432"}
3
Answers
Bash doesn’t have json support. You must use a command line
jq
to process the output.For example:
echo "$test" | jq .test
https://jqlang.github.io/jq/ is your friend.
Simply use
echo "$test" | jq .test
.If
$test
is really an (exported) "environment variable", you can use jq’senv
or$ENV
builtins to access it, avoiding theecho
subshell: