# How to store a string from console output to environmental variable?

**URL:** <https://community.jenkins.io/t/how-to-store-a-string-from-console-output-to-environmental-variable/5197>\
**Category:** Ask a question\
**Tags:** question\
**Created:** [January 4, 2023, 8:49am UTC](https://community.jenkins.io/t/how-to-store-a-string-from-console-output-to-environmental-variable/5197 "2023-01-04T08:49:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surath](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Surath](https://community.jenkins.io/u/Surath)\
**Post date:** [January 4, 2023, 8:49am UTC](https://community.jenkins.io/t/how-to-store-a-string-from-console-output-to-environmental-variable/5197/1 "2023-01-04T08:49:11Z")

</div>

I had a scenario where I have created a freestyle project and adding build steps. In build step 1 I am calling an API using HTTP request Plugin and in the console I am getting the token which I need to pass to build step 2.  
_Please help on this._

**Thanks**

---

<div class="post-metadata">

**Author:** ![poddingue](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/poddingue/32/985_2.png) [@poddingue](https://community.jenkins.io/u/poddingue)\
**Post date:** [January 4, 2023, 9:17am UTC](https://community.jenkins.io/t/how-to-store-a-string-from-console-output-to-environmental-variable/5197/2 "2023-01-04T09:17:43Z")

</div>

Hello @Surath and welcome to this community 👋

Why is the token written on the console?  
Couldn’t you store it in a variable?  
With the [HTTP Request Plugin](https://www.jenkins.io/doc/pipeline/steps/http_request/) you get a `response` object which contains the status and the content.  
I’m sure there are way better approaches, but my first thought was to store the token in a temporary file, and then read the file content in the next step.

Could we see your code?

---

<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 4, 2023, 9:21am UTC](https://community.jenkins.io/t/how-to-store-a-string-from-console-output-to-environmental-variable/5197/3 "2023-01-04T09:21:15Z")

</div>

With freestyle you can’t really. There might be plugins to read a file from disk into a variable, so you could write your output of your command to a file first.

I would really recommend looking into pipeline. It not only has the advantage of having your job configuration as code which can be committed and versioned, but gives you way more flexibility that. The ironically named freestyle

---

<div class="post-metadata">

**Author:** ![Surath](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Surath](https://community.jenkins.io/u/Surath)\
**Post date:** [January 4, 2023, 9:28am UTC](https://community.jenkins.io/t/how-to-store-a-string-from-console-output-to-environmental-variable/5197/4 "2023-01-04T09:28:29Z")

</div>

Hello @poddingue Thankyou for the response.  
The problem here in build step1 I am calling API which will gives we the access\_token and it’s printing on the console now I need to get the access\_token string and store it variable how could I do that ?

---

<div class="post-metadata">

**Author:** ![Surath](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Surath](https://community.jenkins.io/u/Surath)\
**Post date:** [January 4, 2023, 9:30am UTC](https://community.jenkins.io/t/how-to-store-a-string-from-console-output-to-environmental-variable/5197/5 "2023-01-04T09:30:02Z")

</div>

Thankyou @halkeye for quick response. I would like to use freestyle as requirement.

---

<div class="post-metadata">

**Author:** ![Surath](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Surath](https://community.jenkins.io/u/Surath)\
**Post date:** [January 6, 2023, 7:30am UTC](https://community.jenkins.io/t/how-to-store-a-string-from-console-output-to-environmental-variable/5197/6 "2023-01-06T07:30:21Z")

</div>

Hello @halkeye, is there a way to read a JSON file which is stored under the workspace directory in the project and get the value for a key and store it in the environmental variable so that I need to trigger another job using that variable?

---

<div class="post-metadata">

**Author:** ![Surath](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Surath](https://community.jenkins.io/u/Surath)\
**Post date:** [January 6, 2023, 7:31am UTC](https://community.jenkins.io/t/how-to-store-a-string-from-console-output-to-environmental-variable/5197/7 "2023-01-06T07:31:10Z")

</div>

Hello @poddingue, is there a way to read a JSON file which is stored under the workspace directory in the project and get the value for a key and store it in the environmental variable so that I need to trigger another job using that variable?

---

<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, 8:09am UTC](https://community.jenkins.io/t/how-to-store-a-string-from-console-output-to-environmental-variable/5197/8 "2023-01-06T08:09:10Z")

</div>

As I havnt used freestyle in 5+ years I really have nothing to offer. I think you’ll spend more time trying to work around freestyles limitations than you would learning pipeline.

---

<div class="post-metadata">

**Author:** ![imoutsatsos](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/imoutsatsos/32/162_2.png) [@imoutsatsos](https://community.jenkins.io/u/imoutsatsos)\
**Post date:** [September 26, 2023, 2:18am UTC](https://community.jenkins.io/t/how-to-store-a-string-from-console-output-to-environmental-variable/5197/9 "2023-09-26T02:18:57Z")

</div>

If you were to write your API call in some Groovy code you could use a Groovy builder to do whatever you want with the returned token. Don’t believe anyone that tells you that ‘freestyle jobs’ have limitations. In my opinion, the only limitation is in people’s imagination. 😉
