# How to override environment variables in Execute Shell

**URL:** <https://community.jenkins.io/t/how-to-override-environment-variables-in-execute-shell/3091>\
**Category:** Ask a question\
**Created:** [July 19, 2022, 5:14pm UTC](https://community.jenkins.io/t/how-to-override-environment-variables-in-execute-shell/3091 "2022-07-19T17:14:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbasaran](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/bbasaran/32/1887_2.png) [@bbasaran](https://community.jenkins.io/u/bbasaran)\
**Post date:** [July 19, 2022, 5:14pm UTC](https://community.jenkins.io/t/how-to-override-environment-variables-in-execute-shell/3091/1 "2022-07-19T17:14:11Z")

</div>

Hi everyone,

I have a Jenkins job that feeds a Mattermost chat channel, with output value returned from a Python script, after each build.

I defined an environment variable under Manage Jenkins → Configure System as following:

[![enter image description here](https://europe1.discourse-cdn.com/flex013/uploads/jenkins/original/2X/0/09c1443e3ab840f2172bb61d0e81439054b38bcc.jpeg)](https://i.stack.imgur.com/WVcysm.png)

In “Execute shell”, I call my script and it returns the relevant value as below.

```auto
RET=$(python main.py)

```

However, I could not override the environment variable **SCRIPT\_RET** with the output value **RET** above, to use in the custom message with $SCRIPT\_RET

Any help is appreciated, thanks!

---

<div class="post-metadata">

**Author:** ![martinda](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/martinda/32/288_2.png) [@martinda](https://community.jenkins.io/u/martinda)\
**Post date:** [July 19, 2022, 9:14pm UTC](https://community.jenkins.io/t/how-to-override-environment-variables-in-execute-shell/3091/2 "2022-07-19T21:14:17Z")

</div>

It’s hard to answer that question without more context. For example, can you show how you set SCRIPT\_RET to the value or RET, and where you try to read the SCRIPT\_RET value? That would all have to be in the same context, for example all in the same Freestyle job shell script, or in the same Pipeline Script.

---

<div class="post-metadata">

**Author:** ![bbasaran](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/bbasaran/32/1887_2.png) [@bbasaran](https://community.jenkins.io/u/bbasaran)\
**Post date:** [July 19, 2022, 9:52pm UTC](https://community.jenkins.io/t/how-to-override-environment-variables-in-execute-shell/3091/3 "2022-07-19T21:52:58Z")

</div>

All I want to do is to transfer the output value of my Python script to the post-build action (Mattermost chat hook in this case).

I read the value of SCRIPT\_RET in Post-build actions as following:

 ![Screenshot 2022-07-19 at 23.40.55](https://europe1.discourse-cdn.com/flex013/uploads/jenkins/original/2X/6/6758da37fa87bcb1bb7e6bd4bf9fdc9d587f46e9.png)

The problem is that I could not override this environment variable with the value returned from my Python script that I call in the Freestyle job shell script as you see below.

[URL to the img (not allowed to directly upload the 2nd image)](https://postimg.cc/9RvrJ8zJ)

This simply does not recognise the environment variable, but treats it as a regular variable. As a result, SCRIPT\_RET in the post-build action remains null, as I do not assign something to it at the beginning while creation.

---

<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:** [July 19, 2022, 11:08pm UTC](https://community.jenkins.io/t/how-to-override-environment-variables-in-execute-shell/3091/4 "2022-07-19T23:08:37Z")

</div>

I strongly recommend looking into swapping out to pipeline, you’ll be able to do this, and have much more freedom than a freestyle job (which is now ironically named).

That being said, If i remember my freestyle correctly, you’ll have to execute a shell script, write it to a file, and then use a different plugin to import into your envs. I’m not sure said plugin actually exists. I wrote [GitHub - halkeye/jenkins-fileToEnv: Jenkins plugin](https://github.com/halkeye/jenkins-fileToEnv) a long time ago, but never polished it up to release.

just fyi, in pipeline it would be

```auto
script {
   env.SCRIPT_RET=sh(returnStdout: true, script: 'python main.py').trim()
}

```

That being said, you could do a conditional step (to do notify failure custom logic) and just curl to mattermost, googling says its just a simple post and have more freedom that way.

Hope that helps, my freestyle is super rusty

---

<div class="post-metadata">

**Author:** ![bbasaran](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/bbasaran/32/1887_2.png) [@bbasaran](https://community.jenkins.io/u/bbasaran)\
**Post date:** [July 27, 2022, 8:34am UTC](https://community.jenkins.io/t/how-to-override-environment-variables-in-execute-shell/3091/5 "2022-07-27T08:34:15Z")

</div>

I am not able to convert it to a pipeline right now, because it will require a bit time, but I will keep this on my mind when I am able to do so. Thank you for your help!
