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 exported to a shell variable like so:
var=$(jq -n '$ENV')
To remove junk variables like _, SHLVL, etc. from the list you can use the del function.
var=$(jq -n '$ENV | del(._, .SHLVL)')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With