简体   繁体   中英

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.

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)')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM