# Cannot assign meta-data to variable

**URL:** https://forum.buildkite.community/t/cannot-assign-meta-data-to-variable/3344
**Category:** Pipelines
**Created:** [November 7, 2023, 12:53am UTC](https://forum.buildkite.community/t/cannot-assign-meta-data-to-variable/3344 "2023-11-07T00:53:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![xtof](https://avatars.discourse-cdn.com/v4/letter/x/c0e974/32.png) [@xtof](https://forum.buildkite.community/u/xtof)
#### Post date: [November 7, 2023, 12:53am UTC](https://forum.buildkite.community/t/cannot-assign-meta-data-to-variable/3344/1 "2023-11-07T00:53:20Z")

</div>

trying to implement very simple use case…

Group 1, Step

> buildkite-agent meta-data set ENV $DEFAULT\_ENV

Group 2, Step

> buildkite-agent meta-data get ENV  
> var=$(buildkite-agent meta-data get ENV)  
> echo “-\>” $var

Result is

> my-env-value  
> →

It seems that command “buildkite-agent meta-data get” works but I cannot assign it to var  
Any idea ? Thanks

Agent is running on MacOS, buildkite-agent version 3.57.0+x.78b37076ce6362a0c3f700edcbd0bdcf9f22d2d8

---

<div class="post-metadata">

### Author: ![james.s](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/james.s/32/1305_2.png) [@james.s](https://forum.buildkite.community/u/james.s)
#### Post date: [November 7, 2023, 2:16am UTC](https://forum.buildkite.community/t/cannot-assign-meta-data-to-variable/3344/2 "2023-11-07T02:16:50Z")

</div>

Hello @xtof!

Thanks for the question - and welcome to the Buildkite Forum! 👋

I’m assuming you’d want to keep this within the Step editor? Since these commands are all done within processes (with PIDs) - you could do something like this (assuming its a similar approach!) - note the `$$` interpolation:

```auto
  - group: "Group 1"
    key: "group_1"
    steps:
      - label: ":cd: Setting env"
        commands: | 
          buildkite-agent meta-data set env $ENV
  - group: "Group 2"
    key: "group_2"
    depends_on:
    - "group_1"
    steps:
      - label: ":printer: Export/obtaining var1"
        commands: | 
          export var1=$(buildkite-agent meta-data get env)
          echo $$var1

```

Keep in mind, `var1` will only be available to that job itself (from the `export`) - you could run this all in a script to a similar effect too, this is the assumption its all kept inside the group step as above.

Another way you could go around this is using build level environment variables, so something like

```auto
env:
  ENVIRONMENT: production

```

at the start of the pipeline, and use $ENVIRONMENT through the build (this can also be set at the start of the build too, so for example, `ENVIRONMENT: $ENVIRONMENT` (and set `ENVIRONENT=xyz` in the Environment Variables section on a new build through the UI/set in the payload within the API:

![image](https://us1.discourse-cdn.com/flex016/uploads/buildkite1/original/2X/b/b36a3419e5bf2f596e928e908738b2a35087d58d.png)

Hope that helps!

---

<div class="post-metadata">

### Author: ![xtof](https://avatars.discourse-cdn.com/v4/letter/x/c0e974/32.png) [@xtof](https://forum.buildkite.community/u/xtof)
#### Post date: [November 7, 2023, 8:56pm UTC](https://forum.buildkite.community/t/cannot-assign-meta-data-to-variable/3344/3 "2023-11-07T20:56:31Z")

</div>

Thanks @james.s  
It is working : i missed interpolation to manipulate variable after meta-data get  
why do we need it ?

---

<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: [November 7, 2023, 9:27pm UTC](https://forum.buildkite.community/t/cannot-assign-meta-data-to-variable/3344/4 "2023-11-07T21:27:12Z")

</div>

👋 Interpolation happens at upload time, so in this case when you are trying to use $var is actually evaluated before the build starts running and at that time, the step to set the variable hasn’t run, so it ends up being empty.  
You can read more about variable interpolation in our [docs](https://buildkite.com/docs/pipelines/environment-variables#runtime-variable-interpolation)

---

<div class="post-metadata">

### Author: ![xtof](https://avatars.discourse-cdn.com/v4/letter/x/c0e974/32.png) [@xtof](https://forum.buildkite.community/u/xtof)
#### Post date: [November 7, 2023, 9:28pm UTC](https://forum.buildkite.community/t/cannot-assign-meta-data-to-variable/3344/5 "2023-11-07T21:28:39Z")

</div>

understood  
thanks @paula
