Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export environment variables to JSON in Bash

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.

like image 251
Daniyal Basit Khan Avatar asked Jun 17 '26 01:06

Daniyal Basit Khan


1 Answers

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)')
like image 108
oguz ismail Avatar answered Jun 20 '26 10:06

oguz ismail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!