# Setting environment variable from script command for later use in cache name

**URL:** <https://discuss.circleci.com/t/setting-environment-variable-from-script-command-for-later-use-in-cache-name/24036>\
**Category:** Build Environment\
**Created:** [August 3, 2018, 9:52am UTC](https://discuss.circleci.com/t/setting-environment-variable-from-script-command-for-later-use-in-cache-name/24036 "2018-08-03T09:52:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chooban](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/chooban/32/18766_2.png) [@chooban](https://discuss.circleci.com/u/chooban)\
**Post date:** [August 3, 2018, 9:52am UTC](https://discuss.circleci.com/t/setting-environment-variable-from-script-command-for-later-use-in-cache-name/24036/1 "2018-08-03T09:52:38Z")

</div>

I’m using Cypress for some testing in Circle CI and I’d like to be able to cache the downloaded binary so speed up the builds a little bit, and ideally that cache would be based on the installed version number so it “just works” across upgrades.

I’m attempting to extract the version number in one job, and then use it in another.

```yaml
      - run:
          name: Extract Cypress version for cache name
          command: |
            CYPRESS_VERSION=$(yarn info cypress version | grep -v "DeprecationWarning" | sed -n "2p")
            echo $CYPRESS_VERSION
            echo 'export CYPRESS_VERSION=$CYPRESS_VERSION' >> $BASH_ENV
      - restore_cache:
          name: Restoring Cypress cache
          keys:
            - cy-{{ .Environment.CYPRESS_VERSION }}

```

I can see in the job output that it’s getting the correct value for the version, but on restoring the cache it says `Found a cache from build 1476 at cy-<no value>` which, oddly enough, isn’t the correct version number.

Current hypothesis is that the `$BASH_ENV` environment variables aren’t the same as the `.Environment` ones, which does make sense. Is it possible to use bash env vars in cache names?

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/halfer/32/9083_2.png) [@halfer](https://discuss.circleci.com/u/halfer)\
**Post date:** [August 3, 2018, 10:25am UTC](https://discuss.circleci.com/t/setting-environment-variable-from-script-command-for-later-use-in-cache-name/24036/2 "2018-08-03T10:25:33Z")

</div>

In [the docs](https://circleci.com/docs/2.0/caching/), there’s this info:

> The environment variable `variableName` (supports any environment variable [exported by CircleCI](https://circleci.com/docs/2.0/env-vars/#circleci-environment-variable-descriptions) or added to a specific [Context](https://circleci.com/docs/2.0/contexts)—not any arbitrary environment variable).

However, all is not lost; you can use a file instead. In your version capture:

```
echo $CYPRESS_VERSION > /tmp/cypress-version

```

Then in your restore keys:

```
- cy-{{ checksum "/tmp/cypress-version" }}

```

---

<div class="post-metadata">

**Author:** ![chooban](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/chooban/32/18766_2.png) [@chooban](https://discuss.circleci.com/u/chooban)\
**Post date:** [August 6, 2018, 2:14pm UTC](https://discuss.circleci.com/t/setting-environment-variable-from-script-command-for-later-use-in-cache-name/24036/3 "2018-08-06T14:14:24Z")

</div>

This worked brilliantly, thanks!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [November 4, 2018, 2:24pm UTC](https://discuss.circleci.com/t/setting-environment-variable-from-script-command-for-later-use-in-cache-name/24036/4 "2018-11-04T14:24:18Z")

</div>

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