From: Kashyap Ivaturi Date: 2010-09-28T08:25:32+09:00 Subject: Re: passing single backslash character as commang line argument Brian Candler wrote: > Kashyap Ivaturi wrote:0 >> Please let me know how to get the backslash character in the string in >> the test2.rb program. > > That depends. > > If you insist on invoking system with a single string argument, it gets > passed to the shell to execute - something like /bin/sh -c "the string". > > This means the shell breaks it into words, and you have to deal with the > shell's rules for handling quoting and backslashes: > > irb(main):004:0> str = "\\" # one backslash > => "\\" > irb(main):005:0> system("echo \"#{str}\"") > sh: Syntax error: Unterminated quoted string > => false > irb(main):006:0> str = "\\\\" # two backslahses > => "\\\\" > irb(main):007:0> system("echo \"#{str}\"") > \ > => true > > If instead you use the multi-argument form of system, then you don't get > a shell - Ruby invokes the program directly with the arguments given. > This means you don't need to worry about backslashes, or even quoting. > > That is: > > system("/usr/bin/ruby", test2AbsolutePath, text) > > will pass those three args, and you don't need to quote the text even if > it contains spaces, backslashes or whatever. I strongly recommend you go > this way. > > To find the full path to the ruby interpreter, have a look at the > rbconfig library. Brian, I changed the code as you suggested, but I still do not get see the single backslash appearing in the string that gets sent to the test2.rb Here's the code: test1.rb: test2AbsolutePath = "C:\\test2.rb" text = "Sample Ruby '\\' String" system("ruby", test2AbsolutePath, text) test2.rb: text = ARGV[0] puts "text: #{text}" Current Output: text: Sample Ruby '' String Expected Output: text: Sample Ruby '\' String Please let me know what needs to be modified to get the single backslash in the output. -- Posted via http://www.ruby-forum.com/.