From: Ashley Yakeley Date: 2001-05-09T15:10:13+09:00 Subject: [ruby-talk:14897] Backslashes in Backquotes 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: irb(main):001:0> print `echo \1` nil irb(main):002:0> `echo \1` "\001\n" irb(main):003:0> print `echo \\1` \1 nil irb(main):004:0> print `echo '\1'` \ nil irb(main):005:0> print `echo '\\1'` \\1 nil ...which is not what sh does: localhost% echo \1 1 localhost% echo \\1 \1 localhost% echo '\1' \1 localhost% echo '\\1' \\1 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. -- Ashley Yakeley, Seattle WA