# Popen needs escaped pathname?

**URL:** <https://www.ruby-forum.com/t/popen-needs-escaped-pathname/172671>\
**Category:** Ruby\
**Created:** [July 25, 2009, 2:10am UTC](https://www.ruby-forum.com/t/popen-needs-escaped-pathname/172671 "2009-07-25T02:10:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![n1ghtshade3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e19adc/32.png) [@n1ghtshade3](https://www.ruby-forum.com/u/n1ghtshade3)\
**Post date:** [July 25, 2009, 2:10am UTC](https://www.ruby-forum.com/t/popen-needs-escaped-pathname/172671/1 "2009-07-25T02:10:47Z")

</div>

I am using popen to pipe some text through a Perl script and I’ve just  
discovered (accidentally) that the pathname for this script has to be  
explicitly escaped - it must not contain unescaped spaces. This  
surprised me because elsewhere in Ruby this does not appear to be the  
case (in “require”, for example). What is the rule here? Thx - m.

---

<div class="post-metadata">

**Author:** ![n1ghtshade3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e19adc/32.png) [@n1ghtshade3](https://www.ruby-forum.com/u/n1ghtshade3)\
**Post date:** [July 25, 2009, 3:27am UTC](https://www.ruby-forum.com/t/popen-needs-escaped-pathname/172671/2 "2009-07-25T03:27:29Z")

</div>

Matt N. wrote:

> I am using popen to pipe some text through a Perl script and I’ve just  
> discovered (accidentally) that the pathname for this script has to be  
> explicitly escaped - it must not contain unescaped spaces. This  
> surprised me because elsewhere in Ruby this does not appear to be the  
> case (in “require”, for example). What is the rule here? Thx - m.

It’s a shell thing…

pipe = IO.popen(“ls \*”, “r”)

doesn’t look for a program named “ls \*”.

It’s like the difference between

system(“ls \*”)

and

system(“ls”, “\*”)

except that in the case of IO.popen there is no (AFAICT) way to do the  
latter. On platforms with fork() you can emulate it:

IO.popen("-", “r”) do |pipe|  
if pipe  
puts pipe.read  
else  
exec “ls”, “_" # error if there is no file named "_”  
end  
end

---

<div class="post-metadata">

**Author:** ![n1ghtshade3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e19adc/32.png) [@n1ghtshade3](https://www.ruby-forum.com/u/n1ghtshade3)\
**Post date:** [July 25, 2009, 4:00am UTC](https://www.ruby-forum.com/t/popen-needs-escaped-pathname/172671/3 "2009-07-25T04:00:52Z")

</div>

Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Matt N. wrote:
> 
> > I am using popen to pipe some text through a Perl script and I’ve just  
> > discovered (accidentally) that the pathname for this script has to be  
> > explicitly escaped - it must not contain unescaped spaces. This  
> > surprised me because elsewhere in Ruby this does not appear to be the  
> > case (in “require”, for example). What is the rule here? Thx - m.
> 
> It’s a shell thing…

Right, I see; I’m talking directly to the shell…

I think the docs should tell me this!

So the same syntax that works in backticks will work here.

Thx - m.
