Environment variable interpolation not working


I’m trying to interpolate an environment variable within another one as it’s stated in the project configuration UI (Environment variables section).

No matter which variable I use (even the example: ${HOME}) it’s always escaped (\$HOME). I’m using export as a build step to print all the environment variables and debug it.

May we see the whole of your config file? I take it you’re using Circle 2.0?

Note that env vars don’t work in all parts of a file. For example, they do work in commands, but they don’t work in cache key definitions.


here is my configuration file

And here is a screenshot of how I’m trying to add an environment variable:

And here is the result: https://imgur.com/a/9Qwot (I can’t upload more than one image per post with my permissions on this forum)

Thank you for your help

Righto, so you’re trying to set API_URL equal to another variable. I don’t know if that works from the dialogue box, but the help text says it should!

If I were debugging this, I would try these:

  • Swap ${CIRCLE_BRANCH} in that dialogue box for a non-variable value. Does that work? If it does, then maybe this dialogue only works for things that exist forever (e.g. $HOME) and the one you are trying to use is created too late to be interpolated correctly?
  • Consider deleting this var and adding API_URL=${CIRCLE_BRANCH} in an early run step in your config file instead.
  • If that does not work, try putting export in front of it. I am not an expert on env var behaviour, but I believe this can make a difference when wanting env vars to be exposed to new processes.

Let us know how you get on.


Your assumptions are right and after carefully reading the 2.0 docs about this matter I found this: https://circleci.com/docs/2.0/env-vars/#interpolating-environment-variables-to-set-other-environment-variables

So I’m simply writing the variables I need to $BASH_ENV in a build step and I can now access them on later steps.

Thank you once again for your time, really appreciated

1 Like

Great stuff! I believe you can mark your answer above as the solution by clicking the ‘solved’ widget just below it - that may be useful for a future reader.