# Properly source ENV variables

**URL:** https://discourse.julialang.org/t/properly-source-env-variables/6726
**Category:** General Usage
**Tags:** question
**Created:** [October 27, 2017, 5:33pm UTC](https://discourse.julialang.org/t/properly-source-env-variables/6726 "2017-10-27T17:33:18Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)
#### Post date: [October 27, 2017, 5:33pm UTC](https://discourse.julialang.org/t/properly-source-env-variables/6726/1 "2017-10-27T17:33:18Z")

</div>

I am trying to use Julia to automate an interface with a server running ROS. To do this I need to source some environment variables:

```julia
export ROS_MASTER_URI=http://xxx.xxx.xxx.xxx:xxxxx
export ROS_IP=xxx.xxx.xxx.xxx
export ROS_HOSTNAME=xxx.xxx.xxx.xxx

```

where the xxx’s are digits for IP addresses and port numbers.

If I run these commands in the terminal, I can thereafter run commands like `rostopic list` and receive the expected output.  
Running `ENV["ROS_MASTER"] = "http://xxx.xxx.xxx.xxx:xxxxx"` etc. in my script and then running `run(`rostopic list`)` errors out with “ERROR: Unable to communicate with master!” and a pipeline\_error.  
I also tried putting the export commands into my .bashrc and in my .juliarc.jl. These didn’t fix it either.  
Is there something I am missing?

Thanks!

---

<div class="post-metadata">

### Author: ![schmrlng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schmrlng/32/2325_2.png) [@schmrlng](https://discourse.julialang.org/u/schmrlng)
#### Post date: [October 28, 2017, 2:42am UTC](https://discourse.julialang.org/t/properly-source-env-variables/6726/3 "2017-10-28T02:42:28Z")

</div>

Does your script contain `ENV["ROS_MASTER"]` or `ENV["ROS_MASTER_URI"]`?

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [October 28, 2017, 4:07am UTC](https://discourse.julialang.org/t/properly-source-env-variables/6726/4 "2017-10-28T04:07:53Z")

</div>

> I use backticks - not sure how to get those to show up in markdown

Just keep adding more backticks around the string you want to code-quote until it’s right:

```
``run(`rostopic list`)``

```

(to do the above, prepend four spaces).

And yeah, I think @schmrlng is onto something.

---

<div class="post-metadata">

### Author: ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)
#### Post date: [October 28, 2017, 4:43am UTC](https://discourse.julialang.org/t/properly-source-env-variables/6726/5 "2017-10-28T04:43:33Z")

</div>

I have tried setting all three variables in the script:

```julia
ENV["ROS_MASTER_URI"] = "http://xxx.xxx.xxx.xxx:xxxxx"
ENV["ROS_IP"] = "xxx.xxx.xxx.xxx"
ENV["ROS_HOSTNAME"] = "xxx.xxx.xxx.xxx"

```

but otherwise do not use `ENV`.

---

<div class="post-metadata">

### Author: ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)
#### Post date: [October 28, 2017, 8:16am UTC](https://discourse.julialang.org/t/properly-source-env-variables/6726/6 "2017-10-28T08:16:09Z")

</div>

Though this is not recommended (use it with causion), I shamelessly mention [`Shell.jl`](https://github.com/innerlee/Shell.jl). If it is helpful, pls report bugs or errors in the package 🙂

```julia
using Shell

Shell.run("""
export ROS_MASTER_URI=http://xxx.xxx.xxx.xxx:xxxxx
export ROS_IP=xxx.xxx.xxx.xxx
export ROS_HOSTNAME=xxx.xxx.xxx.xxx
rostopic list
""")
```

---

<div class="post-metadata">

### Author: ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)
#### Post date: [October 30, 2017, 8:45pm UTC](https://discourse.julialang.org/t/properly-source-env-variables/6726/8 "2017-10-30T20:45:03Z")

</div>

I get an error: “could not spawn `zsh /tmp/juliawGdwxK`: no such file or directory”.  
Calling `Shell.run("ls")` throws the same error.  
Oh, sorry. Shell.run() takes in a String, not a cmd. It works!

---

<div class="post-metadata">

### Author: ![schmrlng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schmrlng/32/2325_2.png) [@schmrlng](https://discourse.julialang.org/u/schmrlng)
#### Post date: [October 30, 2017, 11:58pm UTC](https://discourse.julialang.org/t/properly-source-env-variables/6726/9 "2017-10-30T23:58:18Z")

</div>

What do you get if you `run(`env`)`? That should print all of the current environment variables. I’m unable to duplicate your issue; if the environment variables show up as you would expect them in `run(`env`)` then I might double-check your ROS setup outside of julia.

```julia
 schmrlng@europa ~ julia
               _
   _ _ _(_)_ | A fresh approach to technical computing
  (_) | (_) (_) | Documentation: https://docs.julialang.org
   _ _ _| |_ __ _ | Type "?help" for help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 0.6.1 (2017-10-24 22:15 UTC)
 _/ |\ __'_|_|_|\__'_| |  
|__/ | x86_64-linux-gnu

julia> ENV["ROS_MASTER_URI"] = "http://127.0.0.1:11311"; ENV["ROS_IP"] = "127.0.0.1"; ENV["ROS_HOSTNAME"] = "127.0.0.1"
"127.0.0.1"

julia> run(`rostopic list`)
/europa
/rosout
/rosout_agg

julia> ENV["ROS_MASTER_URI"] = "http://XXX.XXX.XXX.140:11311"; ENV["ROS_IP"] = "XXX.XXX.XXX.209"; ENV["ROS_HOSTNAME"] = "XXX.XXX.XXX.209"
"XXX.XXX.XXX.209"

julia> run(`rostopic list`)
/ceres
/rosout
/rosout_agg

```

---

<div class="post-metadata">

### Author: ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)
#### Post date: [October 31, 2017, 3:00am UTC](https://discourse.julialang.org/t/properly-source-env-variables/6726/10 "2017-10-31T03:00:13Z")

</div>

You may try `Shell.run(..., shell="bash")` because I default the shell to zsh…

---

<div class="post-metadata">

### Author: ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)
#### Post date: [November 1, 2017, 9:48pm UTC](https://discourse.julialang.org/t/properly-source-env-variables/6726/11 "2017-11-01T21:48:44Z")

</div>

Okay, so it works in the REPL and it works in a standalone script but some other things I am running ahead of that (curl commands) cause it to not work. Seems to be a problem that I introduce.  
Sorry for the false alarm.  
Thanks for everyone’s input.  
Yeah, looks like I just needed a `sleep(1)` command. ☹
