# Can't access environment in windows bat script in pipline

**URL:** <https://community.jenkins.io/t/cant-access-environment-in-windows-bat-script-in-pipline/5234>\
**Category:** Ask a question\
**Created:** [January 6, 2023, 2:29am UTC](https://community.jenkins.io/t/cant-access-environment-in-windows-bat-script-in-pipline/5234 "2023-01-06T02:29:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkking](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/pkking/32/3071_2.png) [@pkking](https://community.jenkins.io/u/pkking)\
**Post date:** [January 6, 2023, 2:29am UTC](https://community.jenkins.io/t/cant-access-environment-in-windows-bat-script-in-pipline/5234/1 "2023-01-06T02:29:56Z")

</div>

```auto
pipeline {
  agent {label 'windows'}
 parameters {
    choice(choices: ['22.03', '20.03', '22.09'], name: 'release') }

  environment {
    release_version = '0'    
  }

  stages {
    stage('prepare') {
      steps {
        script {
          nstdout = bat(returnStdout: true, script: '@python get_sp_version.py %release%').trim()                                
          release_version = nstdout                    
          println("stdout ####" + release_version + "###########")
        }
     bat "echo %release_version%.%BUILD_NUMBER%"
     echo "${release_version}.${BUILD_NUMBER}"
   }
   }
  }
}

```

The code above will output like:

```auto
stdout ####2203.1###########
0.47
2203.1.47

```

2203.1 is the value of the nstdout and release\_version.

My expected output is

```auto
stdout ####2203.1########### 
2203.1.47
2203.1.47

```

Am i missing something?  
IMO the env var can be accessed by the bat script directly, right?

---

<div class="post-metadata">

**Author:** ![PierreBtz](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/pierrebtz/32/55_2.png) [@PierreBtz](https://community.jenkins.io/u/PierreBtz)\
**Post date:** [January 6, 2023, 10:13am UTC](https://community.jenkins.io/t/cant-access-environment-in-windows-bat-script-in-pipline/5234/2 "2023-01-06T10:13:12Z")

</div>

I’m not sure you can override the environment variable value like you do. In the second example with the `echo` you are not reading the environment variable but the variable defined in the pipeline, that might explain the difference.  
Looking at [this documentation](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables) you can dynamically define the variable by calling the `bat` step directly in the `environment` block.

---

<div class="post-metadata">

**Author:** ![halkeye](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/halkeye/32/8_2.png) [@halkeye](https://community.jenkins.io/u/halkeye)\
**Post date:** [January 6, 2023, 6:28pm UTC](https://community.jenkins.io/t/cant-access-environment-in-windows-bat-script-in-pipline/5234/3 "2023-01-06T18:28:10Z")

</div>

> [@PierreBtz](#):
>
> I’m not sure you can override the environment variable value like you do.

`script { env.VARIABLENAME = "foo" }` does work, i’ve used it for a while.

> [@pkking](#):
>
> ` release_version = nstdout `

I’m not sure you can update an env variable without using env., i think it may create a new local variable, which as pierre pointed out, is why the echo statement works ${} is groovy not env
