# Double quotes embedded in a string

**URL:** <https://www.ruby-forum.com/t/double-quotes-embedded-in-a-string/100380>\
**Category:** Ruby\
**Created:** [July 28, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/double-quotes-embedded-in-a-string/100380 "2006-07-28T17:47:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecd19e/32.png) [@dale](https://www.ruby-forum.com/u/dale)\
**Post date:** [July 28, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/double-quotes-embedded-in-a-string/100380/1 "2006-07-28T17:47:45Z")

</div>

Here is something that I am struggling with – I would like to execute a  
command string with an IO.popen call that contains embedded double quote  
characters. When I build the initial command string everything seems to  
work, but once I pass is on to IO.popen, it seems to mess up the double  
quotes.

Here is my example, I want to execute the following command from a Ruby  
program:

```
 pppd connect "chat -v '' ATZ OK ATDT5551234 CONNECT '' ogin:

```

username word: password" /dev/ttyS0 115200 -detach crtscts modem  
defaultroute

I create the command string by the long and somewhat ugly ruby command:

```
 command = "pppd connect \\\"chat -v '' #{initialization_command}

```

#{initialization\_response} #{dial\_command} #{dial\_response} ‘’  
#{username\_prompt} #{username} #{password\_prompt} #{password}\"  
#{device.name} #{speed} -detach crtscts modem defaultroute\r\n"

In this case, I escaped the escaped the double quote. I have also tried  
various combinations of multiple double quotes (4 in a row, etc.).

If I now echo the command using a system call:

```
 system( "echo #{command}" )

```

the command seems to echo the desired result:

```
 pppd connect "chat -v ATZ OK ATD5551234 CONNECT ogin: username

```

word: password" /dev/ttyS0 115200 -detach crtscts modem defaultroute

But when I pass it to IO.popen, I get

```
 pppd: unrecognized option '-v'
 pppd version 2.4.1
   Usage: pppd [options], where options are:
         <device> Communicate over the named device
         <speed> Set the baud rate to <speed>
         <loc>:<rem> Set the local and/or remote interface IP
                         addresses. Either one may be omitted.
         asyncmap <n> Set the desired async map to hex <n>
         auth Require authentication from peer
         connect <p> Invoke shell command <p> to set up the

```

serial line  
crtscts Use hardware RTS/CTS flow control  
defaultroute Add default route through interface  
file Take options from file   
modem Use modem control lines  
mru Set MRU value to for negotiation  
See pppd(8) for more options.

which is caused by that the double quotes are being stripped by  
IO.popen. How do I avoid this?

Thanks,  
Dale M.

---

<div class="post-metadata">

**Author:** ![dale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecd19e/32.png) [@dale](https://www.ruby-forum.com/u/dale)\
**Post date:** [July 28, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/double-quotes-embedded-in-a-string/100380/2 "2006-07-28T18:10:45Z")

</div>

On Sat, 29 Jul 2006, Dale M. wrote:

> username word: password" /dev/ttyS0 115200 -detach crtscts modem defaultroute
> 
> I create the command string by the long and somewhat ugly ruby command:
> 
> ```
> command = "pppd connect \\\"chat -v '' #{initialization_command}
> 
> ```
> 
> #{initialization\_response} #{dial\_command} #{dial\_response} ‘’  
> #{username\_prompt} #{username} #{password\_prompt} #{password}\"  
> #{device.name} #{speed} -detach crtscts modem defaultroute\r\n"

use this

cmd = %W[  
pppd connect

```
 "
   chat -v ''
   #{ initialization_command }
   #{ initialization_response }
   #{ dial_command }
   #{ dial_response }
   ''
   #{ username_prompt }
   #{ username }
   #{ password_prompt }
   #{ password }
 "

 #{ device.name } #{ speed } -detach crtscts modem defaultroute

```

].join.strip

> In this case, I escaped the escaped the double quote. I have also tried  
> various combinations of multiple double quotes (4 in a row, etc.).

using %W or %Q is the way to go.

> If I now echo the command using a system call:
> 
> ```
> system( "echo #{command}" )
> 
> ```
> 
> the command seems to echo the desired result:
> 
> ```
> pppd connect "chat -v ATZ OK ATD5551234 CONNECT ogin: username
> 
> ```
> 
> word: password" /dev/ttyS0 115200 -detach crtscts modem defaultroute

becuase you running it via the shell - to whom your ‘’ mean something.

> But when I pass it to IO.popen, I get

you bypass the shell. so you calling the pppd program with the  
following  
elements of argv

argv[1] = connect  
argv[2] = "chat  
argv[3] = -v  
argv[4] = ‘’  
argv[5] = ATZ

so there are all sorts of problems - you are losing the word  
splitting/grouping that sh/bash normally does to your command, so you  
lose  
your big double quoted string. you are also losing your empty strings  
since  
bash doesn’t get to take a whack at those you are passing two single  
quote  
marks instead.

you can try to build up the command yourself - or you can just let  
bash/sh do  
it for you: assume you build the command as above, then just let sh  
execute it  
for you like so

stdout =  
IO.popen(‘sh’,‘r+’) do |sh|  
sh.puts cmd  
sh.close\_write  
sh.read  
end

> which is caused by that the double quotes are being stripped by  
> IO.popen. How do I avoid this?

just to clarify. it’s not that IO.popen stripes double quotes, it’s  
that bash  
has not interpreted them.

regards.

-a
