# String interpolation not working in julia pipe to julia !?!

**URL:** https://discourse.julialang.org/t/string-interpolation-not-working-in-julia-pipe-to-julia/6385
**Category:** General Usage
**Tags:** question, piping, string-interpolation
**Created:** [October 11, 2017, 12:28am UTC](https://discourse.julialang.org/t/string-interpolation-not-working-in-julia-pipe-to-julia/6385 "2017-10-11T00:28:28Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![TravisA9](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/travisa9/32/357_2.png) [@TravisA9](https://discourse.julialang.org/u/TravisA9)
#### Post date: [October 11, 2017, 12:28am UTC](https://discourse.julialang.org/t/string-interpolation-not-working-in-julia-pipe-to-julia/6385/1 "2017-10-11T00:28:28Z")

</div>

I was playing around a bit here on an idea based on an unanswered question I found on stackoverflow.

- Namely this one: [Julia - pipe to Julia REPL - Stack Overflow](https://stackoverflow.com/questions/39489021/julia-pipe-to-julia-repl)

The following is related and has a more direct answer:

- [Running an external program with sequential inputs from Julia 0.5.1 - Stack Overflow](https://stackoverflow.com/questions/42894447/running-an-external-program-with-sequential-inputs-from-julia-0-5-1)
- Related to this one too: [https://discourse.julialang.org/t/running-an-external-program-with-sequential-inputs](https://discourse.julialang.org/t/running-an-external-program-with-sequential-inputs)

So, I threw together some tests:

```julia
 # This function is borrowed from: gaston_aux.jl
function popen3(cmd::Cmd)
           pin = Base.Pipe()
           out = Base.Pipe()
           err = Base.Pipe()
           r = spawn(cmd, (pin, out, err))
           Base.close_pipe_sync(out.in)
           Base.close_pipe_sync(err.in)
           Base.close_pipe_sync(pin.out)
           Base.start_reading(out.out)
           Base.start_reading(err.out)
           return (pin.in, out.out, err.out, r)
end

```

Then a function to read the output because I couldn’t get anything else to work.

```julia
function readPipe(out)
   if out.buffer.size >0
       s = readavailable(out)
       return string([Char(st) for st in s]...)
   end
    return ""
end

```

I run/start it with this:

```julia
in, out, err, process = popen3(`julia`)

```

Now the fun part…

```julia
julia> write(in, string("a = 3","\n"))
6

julia> readPipe(out)
"3\n"

julia> write(in, string("println(\"hello: $(a)\")","\n"))
ERROR: UndefVarError: a not defined

julia> write(in, string("println(\"hello: $a\")","\n"))
ERROR: UndefVarError: a not defined

julia> write(in, string("println(\"hello: \", a)","\n"))
22

julia> readPipe(out)
"hello: 3\n"

```

So far, everything I’ve tried to run works with the exception of string interpolation. So, the question is… am I doing something wrong or is it broken?

…also If you have any pointers on a better way of doing this please do tell.

**EDIT:** as an afterthought it occurs to me that julia is probably trying to interpolate the string in the current context and there is no “a” defined. Any thoughts on how to prevent this?

---

<div class="post-metadata">

### Author: ![TravisA9](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/travisa9/32/357_2.png) [@TravisA9](https://discourse.julialang.org/u/TravisA9)
#### Post date: [October 11, 2017, 4:43am UTC](https://discourse.julialang.org/t/string-interpolation-not-working-in-julia-pipe-to-julia/6385/2 "2017-10-11T04:43:07Z")

</div>

Ok, for those that may be interested I found the solution.

Example:

```julia
julia> write(in, string(raw"println(\"hello: $a\")", "\n"))
21

julia> readPipe(out)
"hello: 3\n"

```

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [October 11, 2017, 4:11pm UTC](https://discourse.julialang.org/t/string-interpolation-not-working-in-julia-pipe-to-julia/6385/3 "2017-10-11T16:11:47Z")

</div>

Just to explain why: you were trying to interpolate `a` from the parent process while it’s only defined in the child process. You could put a backslash in front of the `$` in either of your original attempts and it would work too.

---

<div class="post-metadata">

### Author: ![TravisA9](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/travisa9/32/357_2.png) [@TravisA9](https://discourse.julialang.org/u/TravisA9)
#### Post date: [October 11, 2017, 4:16pm UTC](https://discourse.julialang.org/t/string-interpolation-not-working-in-julia-pipe-to-julia/6385/4 "2017-10-11T16:16:52Z")

</div>

Cool, thanks for the tip. If I keep playing around with julia I might get good at it someday. Meanwhile, I’m having fun.
