The environment variables can be printed by using export or printenv in bash, but how can we convert the output into JSON format and then store them in a variable.
The environment variables can be printed by using export or printenv in bash, but how can we convert the output into JSON format and then store them in a variable.
JQ does that for you and populates an internal variable called ENV with the result, which can be stored in a shell variable like so:
var=$(jq -n '$ENV')
And to remove junk variables such as _, SHLVL, etc. from the list, you can use JQ's del function.
var=$(jq -n '$ENV | del(._, .SHLVL)')