# How to access env var in 'bat' step?

**URL:** <https://community.jenkins.io/t/how-to-access-env-var-in-bat-step/10469>\
**Category:** Using Jenkins\
**Created:** [October 30, 2023, 4:18pm UTC](https://community.jenkins.io/t/how-to-access-env-var-in-bat-step/10469 "2023-10-30T16:18:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidA2014](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/davida2014/32/88_2.png) [@DavidA2014](https://community.jenkins.io/u/DavidA2014)\
**Post date:** [October 30, 2023, 4:18pm UTC](https://community.jenkins.io/t/how-to-access-env-var-in-bat-step/10469/1 "2023-10-30T16:18:35Z")

</div>

Hi, for declarative pipeline, I know that one can access an environment variable using, for example:

```auto
steps {
    sh """ 
        echo "Build number in sh script: ${env.BUILD_NUMBER}"
    """
}

```

How does one do that in a ‘bat’ step (on Windows)?

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [October 30, 2023, 4:36pm UTC](https://community.jenkins.io/t/how-to-access-env-var-in-bat-step/10469/2 "2023-10-30T16:36:49Z")

</div>

The way to access env variables in batch script is using the %% “operator”. So, something like

`echo "Build number in bat script: %BUILD_NUMBER%"`

---

<div class="post-metadata">

**Author:** ![DavidA2014](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/davida2014/32/88_2.png) [@DavidA2014](https://community.jenkins.io/u/DavidA2014)\
**Post date:** [October 30, 2023, 4:50pm UTC](https://community.jenkins.io/t/how-to-access-env-var-in-bat-step/10469/3 "2023-10-30T16:50:40Z")

</div>

@slide_o_mix Thank you.

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [October 30, 2023, 5:08pm UTC](https://community.jenkins.io/t/how-to-access-env-var-in-bat-step/10469/4 "2023-10-30T17:08:32Z")

</div>

You are very welcome! Hope its all working for you now.

---

<div class="post-metadata">

**Author:** ![DavidA2014](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/davida2014/32/88_2.png) [@DavidA2014](https://community.jenkins.io/u/DavidA2014)\
**Post date:** [November 1, 2023, 4:46pm UTC](https://community.jenkins.io/t/how-to-access-env-var-in-bat-step/10469/5 "2023-11-01T16:46:50Z")

</div>

@slide_o_mix The env variable is being referenced correctly now, but what is the syntax for a groovy variable please? Is the following correct?

```auto
    steps {
        script {
            def variant = "Quasar"

            bat '''
                cmake -G "Visual Studio 16 2019" -A x64 -DPROJECT_VARIANT="%variant%" ..\\..
            '''

```

The syntax:

```auto
"%variant%"

```

isn’t working.

---

<div class="post-metadata">

**Author:** ![DavidA2014](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/davida2014/32/88_2.png) [@DavidA2014](https://community.jenkins.io/u/DavidA2014)\
**Post date:** [November 1, 2023, 5:05pm UTC](https://community.jenkins.io/t/how-to-access-env-var-in-bat-step/10469/6 "2023-11-01T17:05:49Z")

</div>

Actually, I’ve found the answer, thanks.

[https://stackoverflow.com/questions/52863062/access-a-groovy-variable-from-within-bat-step-in-jenkins-pipeline-on-windows-sla](https://stackoverflow.com/questions/52863062/access-a-groovy-variable-from-within-bat-step-in-jenkins-pipeline-on-windows-sla)

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [November 1, 2023, 6:18pm UTC](https://community.jenkins.io/t/how-to-access-env-var-in-bat-step/10469/7 "2023-11-01T18:18:19Z")

</div>

Right, local variables are not put into the environment for a batch script to use. You would need to do `env.variant = "Quasar"` for it to be added to the environment variables for spawned batch file execution. Glad it is working for you now.
