Environment variable from output of command?



Is there a way to set an environment variable from the output of a command? I need a value to be available for several of the commands that will run and was hoping I could do something like TAG: $(script_to_get_tag) so that ${TAG} could be used in all of the commands.


There are multiple ways.

  1. Define the value in an env file and source it by setting BASH_ENV
  2. Set it verbatim and use $(eval echo ${TEST}) to get the real value.

Envars behavior - v1 versus v2

For #1, the value is generated by the script based on git so is it possible to write it to a file and use BASH_ENV if that’s the case?

For #2, isn’t each command run in it’s own shell? So I don’t understand how that will help me.



Option #1 is probably better in this case.


Is there an example of writing a value from a command to a file and using BASH_ENV that I could use as a reference?

  BASH_ENV: ~/.env
echo 'export FOO=$BASH_ENV' > ~/.env


I’m having issues with this. I have the following in my config:

      - run:
          name: Conditionally deploy to ECR
            COMMIT_TAG: $(eval echo ${CIRCLE_SHA1} | cut -c1-7)
          command: |
            if [ "${CIRCLE_BRANCH}" == "port-test" ]; then
              echo "COMMIT_TAG $COMMIT_TAG"

but this isn’t evaluating the command $(echo $CIRCLE_SHA1 | cut -c1-7), it’s just assigning it to $COMMIT_TAG.
The echo output looks like this:

COMMIT_TAG $(eval echo ${CIRCLE_SHA1} | cut -c1-7)

What would be the correct way to assign the output of the command in this instance?


export COMMIT_TAG=$(eval echo ${CIRCLE_SHA1} | cut -c1-7)

You can’t set it in the environment section


@rohara Can you provide a small, complete example that shows how this can be done?

echo 'export COMMIT_TAG=$(eval echo ${CIRCLE_SHA1} | cut -c1-7)' >> ~/.bashrc

Then just set BASH_ENV to ~/.bashrc

(Untested but should work)


This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.