# Escaping backquote in filenames

**URL:** <https://www.ruby-forum.com/t/escaping-backquote-in-filenames/158339>\
**Category:** Ruby\
**Created:** [January 22, 2009, 9:01am UTC](https://www.ruby-forum.com/t/escaping-backquote-in-filenames/158339 "2009-01-22T09:01:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![juba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5daacb/32.png) [@juba](https://www.ruby-forum.com/u/juba)\
**Post date:** [January 22, 2009, 9:01am UTC](https://www.ruby-forum.com/t/escaping-backquote-in-filenames/158339/1 "2009-01-22T09:01:20Z")

</div>

Hi,

I would like to execute call system on a filename with a backquote (`),  
but I don’t know how to escape it.

-\> ls “[Shawn Lee]/10. Shawn Lee`s Ping Pong Orchestra - Bollywood.mp3”  
-rw-r–r-- 1 hobs medias 3,0M 2009-01-18 15:16 [Shawn Lee]/10. Shawn  
Lee`s Ping Pong Orchestra - Bollywood.mp3

> > s = “[Shawn Lee]/10. Shawn Lee`s Ping Pong Orchestra - Bollywood.mp3" => "[Shawn Lee]/10. Shawn Lee`s Ping Pong Orchestra - Bollywood.mp3”
> 
> > `ls "#{s}"`  
> > sh: -c: line 0: unexpected EOF while looking for matching ``’  
> > sh: -c: line 1: syntax error: unexpected end of file

> > s2 = “[Shawn Lee]/10. Shawn Lee`s Ping Pong Orchestra - Bollywood.mp3”  
> > =\> “[Shawn Lee]/10. Shawn Lee`s Ping Pong Orchestra - Bollywood.mp3”
> 
> > `ls "#{s2}"`  
> > sh: -c: line 0: unexpected EOF while looking for matching ``’  
> > sh: -c: line 1: syntax error: unexpected end of file

> > s3 = “[Shawn Lee]/10. Shawn Lee\`s Ping Pong Orchestra - Bollywood.mp3" => "[Shawn Lee]/10. Shawn Lee\\`s Ping Pong Orchestra - Bollywood.mp3”
> 
> > `ls "#{s3}"`  
> > ls: cannot access [Shawn Lee]/10. Shawn Lee`s Ping Pong Orchestra -  
> > Bollywood.mp3: No such file or directory

Any idea ?

Regards,

Julien

---

<div class="post-metadata">

**Author:** ![juba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5daacb/32.png) [@juba](https://www.ruby-forum.com/u/juba)\
**Post date:** [January 22, 2009, 9:35am UTC](https://www.ruby-forum.com/t/escaping-backquote-in-filenames/158339/2 "2009-01-22T09:35:56Z")

</div>

Hi,

At Thu, 22 Jan 2009 16:58:47 +0900,  
Julien B. wrote in [ruby-talk:325582]:

> I would like to execute call system on a filename with a backquote (`),  
> but I don’t know how to escape it.

Escaping depends on platforms.

> -\> ls “[Shawn Lee]/10. Shawn Lee`s Ping Pong Orchestra - Bollywood.mp3”  
> -rw-r–r-- 1 hobs medias 3,0M 2009-01-18 15:16 [Shawn Lee]/10. Shawn  
> Lee`s Ping Pong Orchestra - Bollywood.mp3
> 
> > > s = “[Shawn Lee]/10. Shawn Lee`s Ping Pong Orchestra - Bollywood.mp3" => "[Shawn Lee]/10. Shawn Lee`s Ping Pong Orchestra - Bollywood.mp3”

In 1.8.7 or later, Shellwords.escape exists.

require ‘shellwords’  
`ls #{Shellwords.escape(s)}`

Or, multiple arugment #exec bypasses shell.

IO.popen("-") {|f| f or exec(“ls”, s); f.read}

In 1.9, IO.popen can handle an array argument.

IO.popen([“ls”, s], &:read)

---

<div class="post-metadata">

**Author:** ![juba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5daacb/32.png) [@juba](https://www.ruby-forum.com/u/juba)\
**Post date:** [January 22, 2009, 10:25am UTC](https://www.ruby-forum.com/t/escaping-backquote-in-filenames/158339/3 "2009-01-22T10:25:48Z")

</div>

Nobuyoshi N. wrote:

> > -rw-r–r-- 1 hobs medias 3,0M 2009-01-18 15:16 [Shawn Lee]/10. Shawn  
> > Lee`s Ping Pong Orchestra - Bollywood.mp3
> > 
> > > > s = “[Shawn Lee]/10. Shawn Lee`s Ping Pong Orchestra - Bollywood.mp3" => "[Shawn Lee]/10. Shawn Lee`s Ping Pong Orchestra - Bollywood.mp3”
> 
> In 1.8.7 or later, Shellwords.escape exists.
> 
> require ‘shellwords’  
> `ls #{Shellwords.escape(s)}`  
> I am using 1.8.6.

> Or, multiple arugment #exec bypasses shell.
> 
> IO.popen("-") {|f| f or exec(“ls”, s); f.read}  
> Thanks, it works.
