Build a JSON string with Bash variables

后端 未结 12 1440
感情败类
感情败类 2020-11-29 01:57

I need to read these bash variables into my JSON string and I am not familiar with bash. any help is appreciated.

#!/bin/sh

BUCKET_NAME=testbucket
OBJECT_N         


        
12条回答
  •  天命终不由人
    2020-11-29 02:44

    You could use envsubst:

      export VAR="some_value_here"
      echo '{"test":"$VAR"}' | envsubst > json.json
    

    also it might be a "template" file:

    //json.template
    {"var": "$VALUE", "another_var":"$ANOTHER_VALUE"}
    

    So after you could do:

    export VALUE="some_value_here"
    export ANOTHER_VALUE="something_else"
    cat  json.template | envsubst > misha.json
    

提交回复
热议问题