Home > Blockchain >  Is it possible to populate a variable in a literal string using bash
Is it possible to populate a variable in a literal string using bash

Time:11-08

I am currently getting a value from dynamodb and storing it as an env var which is the s3 bucket name of where i store my terraform state which is tf-state-$ENV.

I am wanting to substitute $ENV in the string with another environment variable i have set inside of jenkins for the relevant environment e.g. dev, test etc as this is the only difference between the environments.

export tf_bucket=$(aws dynamodb --region eu-west-1 get-item --table-name terraform-jenkins-params --key '{"module": {"S": "compute"}}'| jq '.Item.tf_s3_bucket[]')

echo $tf_bucket 
tf-state-$ENV

echo $ENV
dev

Wanted output
echo $tf_bucket
tf-state-dev

This can be done either using bash or in jenkins, I was trying to avoid using sed etc if possible.

CodePudding user response:

Use bash pattern substitution:

tf_bucket=${tf_bucket//"\$ENV"/$ENV}

This replaces all occurences of $ENV.

A single slash replaces only the first occurence:

tf_bucket=${tf_bucket/"\$ENV"/$ENV}

edit:

This could introduce a code injection vulnerability, but it's possible to use eval to actually expand any variables in the string:

eval "tf_bucket=$tf_bucket"
echo "$tf_bucket"
# gives
tf-state-dev

Or similarly, use $tf_bucket with bash -c:

export ENV
bash -c "echo $tf_bucket"
# gives
tf-state-dev

CodePudding user response:

Piping through envsubst as suggested by @gordon-davisson worked a treat, thanks for all of your suggestions!

CodePudding user response:

You can use jq directly for this, like in this example:

test.json

{
  "Item": {
    "tf_s3_bucket": [
        "tf-state-$ENV"
    ]
  }
}

in bash:

export ENV=foo
jq '.Item.tf_s3_bucket[]|sub("\\$ENV";$ENV["ENV"])' test.json

Note: Don't get confused by the doubly use of ENV in the above example. That's because $ENV is a reserved variable in jq, containing an array with all environment variables. Because you named your variable also ENV, it's $ENV['ENV']

  • Related