# How to change the value of environment variable in logstash?

**URL:** <https://discuss.elastic.co/t/how-to-change-the-value-of-environment-variable-in-logstash/308283>\
**Category:** Logstash\
**Created:** [June 27, 2022, 6:03pm UTC](https://discuss.elastic.co/t/how-to-change-the-value-of-environment-variable-in-logstash/308283 "2022-06-27T18:03:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thuy\_Le1](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@Thuy\_Le1](https://discuss.elastic.co/u/Thuy_Le1)\
**Post date:** [June 27, 2022, 6:03pm UTC](https://discuss.elastic.co/t/how-to-change-the-value-of-environment-variable-in-logstash/308283/1 "2022-06-27T18:03:48Z")

</div>

How to change the value of environment variable in logstash?

ruby {  
code =\> "ENV[test] = 1000"  
}  
=\> doesnot work

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [June 27, 2022, 6:38pm UTC](https://discuss.elastic.co/t/how-to-change-the-value-of-environment-variable-in-logstash/308283/2 "2022-06-27T18:38:27Z")

</div>

> [@Thuy\_Le1](#):
>
> How to change the value of environment variable in logstash?

You can change the ENV variable that Ruby uses. Those changes will persist across multiple ruby filters. So

```
    ruby { code => 'ENV["test"] = "1000"' }
    ruby { code => 'puts ENV["test"]' }
    mutate { add_field => { "test" => "${test:1}" } }

```

will print

```
1000

```

but use the default for the environment substitution

```
      "test" => "1",

```

I am unsure whether the substitution does not work because it is happening in Java rather than Ruby, or because it has already been done before the pipeline starts executing. Either way, it does not work

```
    ruby { code => 'ENV.delete("PATH")' }
    ruby { code => 'puts ENV["PATH"]' }
    mutate { add_field => { "path" => "${PATH:1}" } }

```

will print an empty string, but set [path] to "/usr/..."

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 25, 2022, 6:39pm UTC](https://discuss.elastic.co/t/how-to-change-the-value-of-environment-variable-in-logstash/308283/3 "2022-07-25T18:39:23Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
