# Using Julia to set env variables

**URL:** <https://discourse.julialang.org/t/using-julia-to-set-env-variables/25492>\
**Category:** General Usage\
**Created:** [June 20, 2019, 6:31pm UTC](https://discourse.julialang.org/t/using-julia-to-set-env-variables/25492 "2019-06-20T18:31:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [June 20, 2019, 6:31pm UTC](https://discourse.julialang.org/t/using-julia-to-set-env-variables/25492/1 "2019-06-20T18:31:29Z")

</div>

I’d like to set environment variables in the enclosing shell from a Julia script. I’d have thought I could do

```julia
run(`export TESTVAR=varname`)

```

but I got the following very unexpected error

```julia
ERROR: IOError: could not spawn `export TESTVAR=varname`: no such file or directory (ENOENT)
Stacktrace:
 [1] _spawn_primitive(::String, ::Cmd, ::Array{Any,1}) at ./process.jl:400
 [2] setup_stdios(::getfield(Base, Symbol("##505#506")){Cmd}, ::Array{Any,1}) at ./process.jl:413
 [3] _spawn at ./process.jl:412 [inlined]
 [4] #run#515(::Bool, ::Function, ::Cmd) at ./process.jl:725
 [5] run(::Cmd) at ./process.jl:724
 [6] top-level scope at none:0

```

Anyone know what’s going on here?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 20, 2019, 6:43pm UTC](https://discourse.julialang.org/t/using-julia-to-set-env-variables/25492/2 "2019-06-20T18:43:56Z")

</div>

I think this arises from the fact that `export` is not a external command (i.e., a binary resting in some path, if you do `which export` you will see that the command `which` does not find the `export` binary) but an internal builtin command of the shell (if you do `man bash`, or `man your-shell-here`, you will see it described as a builtin command).

I am not sure how I would solve that, but maybe doing something as:

```julia
run(`bash -e """
export TESTVAR=varname
<the rest of your code there?>
"""`)

```

But probably the best would be use Julia specific commands for dealing with environment variables: [Environment Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/environment-variables/index.html)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 20, 2019, 8:03pm UTC](https://discourse.julialang.org/t/using-julia-to-set-env-variables/25492/3 "2019-06-20T20:03:03Z")

</div>

Is your goal to be able to do something like this?

```julia
$ echo $FOO 
<nothing here>
$ julia my_script.jl

$ echo $FOO
<somehow FOO is now set>

```

If so, then I believe that’s impossible, not just for Julia but for any program. Every program (like Julia) inherits a copy of its parent’s environment variables, so there’s no way to modify environment variables in the parent shell.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [June 20, 2019, 8:05pm UTC](https://discourse.julialang.org/t/using-julia-to-set-env-variables/25492/4 "2019-06-20T20:05:14Z")

</div>

I suppose that makes sense. If that’s the case, I probably need to reconsider what I’m doing, and make everything work via `ENV` only from within Julia.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [June 21, 2019, 9:19am UTC](https://discourse.julialang.org/t/using-julia-to-set-env-variables/25492/5 "2019-06-21T09:19:36Z")

</div>

Depending on what you’re trying to achieve, e.g. if Julia is useful to perform a lot of data treatment which ultimately produce a single (but potentially very complex) value for an environment variable, then something like this might be useful:

```julia
> echo $FOO

> cat foo.jl
println("my very complex result")

> export FOO=$(julia foo.jl)

> echo $FOO
my very complex result

```

In another, more complex variant, you can have julia output a snippet of shell that sets the correct environment variables (AFAIK this is what [lmod](https://lmod.readthedocs.io/en/latest/) does, for example)

```julia
> echo $FOO

> cat foo.jl
println("FOO=\"my very complex result\"")

> eval $(julia foo.jl)

> echo $FOO
my very complex result

```

I wouldn’t recommand this last variant unless you really know what you’re doing: you’ll have to make sure that evaluating the output of your julia script in the shell never does anything stupid.

In any case, if you can re-design your solution so that everything happens in Julia (or in subprocesses of the Julia process), then it would be much better.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 21, 2019, 10:04am UTC](https://discourse.julialang.org/t/using-julia-to-set-env-variables/25492/6 "2019-06-21T10:04:28Z")

</div>

I don’t know the details of your problem, but an environment variable could point to a _file_, which you could use to interchange data.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [June 21, 2019, 1:06pm UTC](https://discourse.julialang.org/t/using-julia-to-set-env-variables/25492/7 "2019-06-21T13:06:24Z")

</div>

Yeah, I suppose my original idea was a bad one. I wanted to have a single Julia script that could do a whole bunch of things to set up “an environment”. Since this would largely be just to run more Julia code anyway, I probably just need a somewhat more sophisticated Julia command line program than I had originally envisioned.
