Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to expand variables with spaces in bash

I’d like to call kubectl run that allows to specify environment values with the syntax: --env=nameOfVar=valueOfVar.

I have this construct:

env=()
for v in var1 var2 var2; do
  env+="--env=$v=${!v}"
done
kubectl run mycontainer "${env[@]}"

In this way, I can pass the value of existing variables to kubectl.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It works ok, except when the value of a var contains spaces.

How can I delimiter arguments correctly to kubectl?

I’ve tried other variations like:

env=
for v in var1 var2 var2; do
  env="$env --env=$v=${!v}"
done
kubectl run mycontainer "$env"

Suposing var1 has the value value 1 and so on, I’d like this result:

kubectl run mycontainer "--env=var1=value 1" "--env=var2=value 2" "--env=var3=value 3"

instead of this:

kubectl run mycontainer --env=var1=value 1 --env=var2=value 2 --env=var3=value 3

>Solution :

Use an array instead.

env=()
for v in var1 var2 var2; do
  env+=("--env=$v=${!v}")
done
kubectl run mycontainer "${env[@]}"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading