# Interpolation issues in a backticks expression

**URL:** https://discourse.julialang.org/t/interpolation-issues-in-a-backticks-expression/9612
**Category:** General Usage
**Created:** [March 9, 2018, 5:25pm UTC](https://discourse.julialang.org/t/interpolation-issues-in-a-backticks-expression/9612 "2018-03-09T17:25:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [March 9, 2018, 5:25pm UTC](https://discourse.julialang.org/t/interpolation-issues-in-a-backticks-expression/9612/1 "2018-03-09T17:25:53Z")

</div>

I’m trying to build this command:

```
 osascript -e 'tell application "Terminal" to do script "julia -i $path/file.jl"'

```

Where `$path` is a julia variable, But I can’t manage to get it to work, any idea ?

Thanks.

---

<div class="post-metadata">

### Author: ![Simon\_Bolland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simon_bolland/32/8920_2.png) [@Simon\_Bolland](https://discourse.julialang.org/u/Simon_Bolland)
#### Post date: [March 10, 2018, 11:23am UTC](https://discourse.julialang.org/t/interpolation-issues-in-a-backticks-expression/9612/2 "2018-03-10T11:23:05Z")

</div>

Seems to be single quotes that are preventing variable expansion in command.  
Escaping them expands $path in command

```julia
path="/tmp/path"
cmd=`osascript -e \'tell application "Terminal" to do script "julia -i $path/file.jl"\'`
julia> dump(cmd)
Cmd
  exec: Array{String}((9,))
    1: String "osascript"
    2: String "-e"
    3: String "'tell"
    4: String "application"
    5: String "Terminal"
    6: String "to"
    7: String "do"
    8: String "script"
    9: String "julia -i /tmp/path/file.jl'"
  ignorestatus: Bool false
  flags: UInt32 0
  env: Void nothing
  dir: String ""

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 10, 2018, 1:05pm UTC](https://discourse.julialang.org/t/interpolation-issues-in-a-backticks-expression/9612/3 "2018-03-10T13:05:10Z")

</div>

> [@jonathanBieler](#):
>
> osascript -e 'tell application “Terminal” to do script “julia -i $path/file.jl”

The problem here is that you have to know how escaping rules work for `osascript`. For example, what if the `path` contains a space, a quote, a dollar sign, or some other special character? Assuming it uses standard shell-like escaping, you can maybe do (I don’t really know `osascript`) something like:

```julia
p = sprint(escape_string, joinpath(path, "file.jl"), "\$!")
s = "tell application \"Terminal\" to do script \"julia -i \\\"$p\\\"\""
`osascript -e $s`

```

Note that you want to pass the argument to `-e` as a single string. No additional quotes are needed around the string, because the `osascript` command is not being executed in the shell.

Avoiding this kind of escaping issue is precisely the reason that backtick-commands in Julia avoid the shell, so you are re-opening that can of worms by passing through _two_ layers of shell: one in the `osascript` command that you are trying to construct, and another in the shell command executed by the `osascript` command. (I may be missing another level of backslashes in the nested shell command above! You’ll need to test things out.)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 10, 2018, 1:22pm UTC](https://discourse.julialang.org/t/interpolation-issues-in-a-backticks-expression/9612/4 "2018-03-10T13:22:57Z")

</div>

I just tried the code I suggested, and an extra level of backslashes is required for filenames containing `$`:

```julia
using Compat
sescape(s) = replace(sprint(escape_string, s, "\$!"), "\\"=>"\\\\")

p = sescape(joinpath(path, "file.jl"))
s = "tell application \"Terminal\" to do script \"julia -i \\\"$p\\\"\""
run(`osascript -e $s`)

```

Again, all these backslashes are essentially because you are going through three levels of escaping: Julia literal string escaping, osascript’s string escaping, and the shell escaping.

(Even this might not be enough escaping for file names containing `"` characters, I haven’t tried that.)

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [March 10, 2018, 4:02pm UTC](https://discourse.julialang.org/t/interpolation-issues-in-a-backticks-expression/9612/5 "2018-03-10T16:02:37Z")

</div>

That works yes, thanks!
