# How to pass variables/data between Commands

**URL:** <https://forum.buildkite.community/t/how-to-pass-variables-data-between-commands/1682>\
**Category:** Pipelines\
**Created:** [August 6, 2021, 11:16am UTC](https://forum.buildkite.community/t/how-to-pass-variables-data-between-commands/1682 "2021-08-06T11:16:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mitch](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mitch](https://forum.buildkite.community/u/Mitch)\
**Post date:** [August 6, 2021, 11:16am UTC](https://forum.buildkite.community/t/how-to-pass-variables-data-between-commands/1682/1 "2021-08-06T11:16:21Z")

</div>

Hi guys,

How can I create/modify a variable in a command and this be accessed in a later command? (Using Linux/Bash/Shell scripts)

e.g.

steps:

- commands: |  
\<define/modify VAR here in command or inside .sh file\>
- wait
- commands: |  
\<access VAR here here in command or inside .sh file\>

After some reading around subshells etc. it seems like this may be unavoidable. Things I’ve tried:

- Using ENV variables outside of step level, but can’t modify these
- Using a VARS.sh as source and trying to modify the var
- Appending new variables to VARS.sh source and trying to access these in another command

Unless I’m using the wrong syntax there doesn’t seem to be a way to do this, with the BK environment variables being the closest to what I need. Does anyone have any advice?

Thanks for any help  
Mitch

EDIT: For anyone else looking, using the meta-data command works!

> **[buildkite-agent meta-data v3](https://buildkite.com/docs/agent/v3/cli-meta-data)**
>
> The Buildkite Agent's meta-data command provides your build pipeline with a powerful key/value data-store that works across build steps and build agents, no matter the machine or network.

(Although not sure if it’s the correct solution it works for now)

---

<div class="post-metadata">

**Author:** ![paula](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/paula/32/954_2.png) [@paula](https://forum.buildkite.community/u/paula)\
**Post date:** [August 6, 2021, 4:10pm UTC](https://forum.buildkite.community/t/how-to-pass-variables-data-between-commands/1682/2 "2021-08-06T16:10:01Z")

</div>

Hi @Mitch!

Welcome to the community! 😊

As you mentioned, using [meta-data](https://buildkite.com/docs/agent/v3/cli-meta-data) would allow you to store data that works across build steps and build agents. Another alternative is using [artifacts](https://buildkite.com/docs/pipelines/artifacts); you can upload and download artifacts between steps which will allow you to retain information between steps, i.e folders. There is an example of this [here](https://github.com/buildkite/artifacts-example) . But meta-data would be the best option for your use-case.

Cheers!

---

<div class="post-metadata">

**Author:** ![poltj18](https://avatars.discourse-cdn.com/v4/letter/p/46a35a/32.png) [@poltj18](https://forum.buildkite.community/u/poltj18)\
**Post date:** [August 7, 2022, 1:18am UTC](https://forum.buildkite.community/t/how-to-pass-variables-data-between-commands/1682/3 "2022-08-07T01:18:14Z")

</div>

Hi Paula, is there a recommended method for sharing secrets between commands? For example, if the first step retrieves a secret and the second step needs to use that secret.

---

<div class="post-metadata">

**Author:** ![suma](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/suma/32/1306_2.png) [@suma](https://forum.buildkite.community/u/suma)\
**Post date:** [August 8, 2022, 5:29pm UTC](https://forum.buildkite.community/t/how-to-pass-variables-data-between-commands/1682/4 "2022-08-08T17:29:37Z")

</div>

Hi @poltj18

First of all welcome to Buildkite community and thank for your question.

If goal is to share secrets or access secrets for steps in a pipeline then best approach is [using secret storage](https://buildkite.com/docs/pipelines/secrets#using-a-secrets-storage-service) such AWS secrets manager, hashicorp vault etc

If you do not use secrets storage services then next best option would be to [use environment hooks](https://buildkite.com/docs/pipelines/secrets#exporting-secrets-with-environment-hooks) so that agent can export the secrets to a step in pipeline.

Please let me know if this helps or if you have any further questions.

---

<div class="post-metadata">

**Author:** ![paula](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/paula/32/954_2.png) [@paula](https://forum.buildkite.community/u/paula)\
**Post date:** [August 29, 2023, 5:39am UTC](https://forum.buildkite.community/t/how-to-pass-variables-data-between-commands/1682/5 "2023-08-29T05:39:31Z")

</div>


