From: Hee-Sob Park Date: 2001-05-09T18:10:09+09:00 Subject: [ruby-talk:14905] Re: Backslashes in Backquotes "Ashley Yakeley" wrote in message news:ashley-F346B5.22342308052001@news.halcyon.com... > Is there an easy way in Ruby to execute a string as a command in a > subshell and return the output text? 'System' doesn't seem to have this > kind of redirection of output. > > I know, I'm probably missing something obvious. But I've been through > the documentation and I couldn't find it... > > My first attempt looked like this: > > def subshell(command) > `command` > end > > That doesn't work, because while `` does execute in a subshell, it also > (annoying) quotes its content like a string literal. So I tried this: > > def subshell(command) > eval '`'+command+'`' > end > > You'd think that would work, right? But no, Ruby insists on doing this > weird control-character interpretation of \1, \2 etc. Try this from irb: > ... > > So, there's no obvious way of passing the string "echo \1" to the shell. > This is a trivial example, but it's a real pain for something like a > regular expression passed as a command-line argument to sed. > Try this: def subshell(command) puts `#{command}` end subshell('echo \1') > -- > Ashley Yakeley, Seattle WA Park Hee-Sob