# Writing to file removes escape char

**URL:** https://discourse.julialang.org/t/writing-to-file-removes-escape-char/33637
**Category:** General Usage
**Created:** [January 21, 2020, 8:36pm UTC](https://discourse.julialang.org/t/writing-to-file-removes-escape-char/33637 "2020-01-21T20:36:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [January 21, 2020, 8:36pm UTC](https://discourse.julialang.org/t/writing-to-file-removes-escape-char/33637/1 "2020-01-21T20:36:47Z")

</div>

I have this string `s`

```julia
julia> s = "abc.\$foo"

julia> s
"abc.\$foo"

```

When I write it to file, this happens:

```julia
julia> open("moo.jl", "w") do io
       write(io, s)
       end
8

```

And in `moo.jl` I have:

```julia
abc.$foo

```

So I get `$foo` instead of `\$foo`. Is this expected? If so, what is the way to preserve the escape char?

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [January 21, 2020, 8:52pm UTC](https://discourse.julialang.org/t/writing-to-file-removes-escape-char/33637/2 "2020-01-21T20:52:17Z")

</div>

Yes this is expected.The \$ causes a dollar sign to be inserted into the string rather than doing the normal string replacement.

If you want to write `abc.\$foo` to a file you would need to declare the string like “abc.\\\$foo”. Or maybe you could do something like string(“abc.”, “\\”, “$”, “foo”) if that would be easier to read. You need the double slash to avoid escaping the end quote…

---

<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: [January 21, 2020, 8:58pm UTC](https://discourse.julialang.org/t/writing-to-file-removes-escape-char/33637/3 "2020-01-21T20:58:55Z")

</div>

Another way of putting what @pixel27 said is that writing to the file didn’t change anything. Your string _never_ contained a `\` character:

```julia
julia> s = "abc.\$foo"
"abc.\$foo"

julia> collect(s)
8-element Array{Char,1}:
 'a'
 'b'
 'c'
 '.'
 '$'
 'f'
 'o'
 'o'

```

Julia is printing a `\` because it’s trying to helpfully show you what you would need to input to re-create the string you have. You need the `\$` to get the literal `$` in the resulting string.

---

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [January 21, 2020, 9:04pm UTC](https://discourse.julialang.org/t/writing-to-file-removes-escape-char/33637/4 "2020-01-21T21:04:01Z")

</div>

Yes, I see, makes sense! Thanks!

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 21, 2020, 9:36pm UTC](https://discourse.julialang.org/t/writing-to-file-removes-escape-char/33637/5 "2020-01-21T21:36:14Z")

</div>

And to show the same point in a different way:

```julia
julia> s = "abc.\$foo";
"abc.\$foo"

julia> print(s)
abc.$foo

```

---

<div class="post-metadata">

### Author: ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)
#### Post date: [January 21, 2020, 11:16pm UTC](https://discourse.julialang.org/t/writing-to-file-removes-escape-char/33637/6 "2020-01-21T23:16:11Z")

</div>

You can also use a raw string literal if you don’t want characters inside it to be escaped:

```julia
julia> collect(raw"abc.\$foo")
9-element Array{Char,1}:
 'a'
 'b'
 'c'
 '.'
 '\\'
 '$'
 'f'
 'o'
 'o'

```

---

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [January 22, 2020, 2:31pm UTC](https://discourse.julialang.org/t/writing-to-file-removes-escape-char/33637/7 "2020-01-22T14:31:00Z")

</div>

Thanks - yes, that makes total sense, of course the `\$` simply escapes the interpolation. I was trapped in between the JS and JL worlds where I was generating JL files which contain Julia functions which generate JS code which contains `$` variables and at some point my brain turned off. 🙂
