# Shellwords preserve backslash

**URL:** <https://www.ruby-forum.com/t/shellwords-preserve-backslash/175135>\
**Category:** Ruby\
**Created:** [September 8, 2009, 4:37pm UTC](https://www.ruby-forum.com/t/shellwords-preserve-backslash/175135 "2009-09-08T16:37:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-jb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@S-jb](https://www.ruby-forum.com/u/S-jb)\
**Post date:** [September 8, 2009, 4:37pm UTC](https://www.ruby-forum.com/t/shellwords-preserve-backslash/175135/1 "2009-09-08T16:37:01Z")

</div>

I am trying to parse a string that is formatted with DOS-like file  
names.  
For instance:

s = “a b c d “C:\user\sub\_dir” “C:\user\other\_dir””

When I use Shellwords.shellwords(s) however, I get the following:

Shellwords.shellwords(s)  
=\> [“a”, “b”, “c”, “d”, “C:usersub\_dir”, “C:userother\_dir”]

I have also tried Shellwords.escape(s) which yields:

Shellwords.escape(s)  
=\> “a\ b\ c\ d\ \“C:\\user\\sub\_dir\”\  
\“C:\\user\\other\_dir\””

Which is not parsed correctly by shellwords (I get back a string rather  
than an array). What I would like to do is is to preserve the  
“C:\user\sub\_dir” format and I am stumped on how to do so. I tried a  
gsub on the string before sending it to shellwords but that seemed to  
preserve nothing. Something similar to

Shellwords.shellwords(s.gsub(’\’, ‘\\\’))  
=\> [“a”, “b”, “c”, “d”, “C:usersub\_dir”, “C:userother\_dir”]

It might be of interest that Shellwords.shellwords(s) worked in my  
version of 1.8.7. I just updated to the latest stable release (1.9.1)  
and ran into this roadblock.  
Searching the list logs turned up little.  
Any suggestions?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![S-jb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@S-jb](https://www.ruby-forum.com/u/S-jb)\
**Post date:** [September 8, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/shellwords-preserve-backslash/175135/2 "2009-09-08T21:22:02Z")

</div>

-j b- wrote:

> I am trying to parse a string that is formatted with DOS-like file  
> names.  
> For instance:
> 
> s = “a b c d “C:\user\sub\_dir” “C:\user\other\_dir””
> 
> When I use Shellwords.shellwords(s) however, I get the following:
> 
> Shellwords.shellwords(s)  
> =\> [“a”, “b”, “c”, “d”, “C:usersub\_dir”, “C:userother\_dir”]
> 
> I have also tried Shellwords.escape(s) which yields:
> 
> Shellwords.escape(s)  
> =\> “a\ b\ c\ d\ \“C:\\user\\sub\_dir\”\  
> \“C:\\user\\other\_dir\””
> 
> Which is not parsed correctly by shellwords (I get back a string rather  
> than an array). What I would like to do is is to preserve the  
> “C:\user\sub\_dir” format and I am stumped on how to do so. I tried a  
> gsub on the string before sending it to shellwords but that seemed to  
> preserve nothing. Something similar to
> 
> Shellwords.shellwords(s.gsub(’\’, ‘\\\’))  
> =\> [“a”, “b”, “c”, “d”, “C:usersub\_dir”, “C:userother\_dir”]
> 
> It might be of interest that Shellwords.shellwords(s) worked in my  
> version of 1.8.7. I just updated to the latest stable release (1.9.1)  
> and ran into this roadblock.  
> Searching the list logs turned up little.  
> Any suggestions?
> 
> Thanks in advance.

I don’t have a solution, but I see the same malfunction moving from ruby  
1.8.6 to ruby 1.9.1p243.

# r1test.rb:

require ‘shellwords’

s = “a b c d “C:\user\sub\_dir” “C:\user\other\_dir””  
s = s.gsub(’\’, ‘\\\’)  
p s

# p Shellwords.shellwords(s)

$ ruby -v  
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.11.1]

$ ruby r1test.rb  
“a b c d “C:\\user\\sub\_dir” “C:\\user\\other\_dir””  
[“a”, “b”, “c”, “d”, “C:\user\sub\_dir”, “C:\user\other\_dir”]

$ ruby19 -v  
ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-darwin8.11.1]

$ ruby19 r1test.rb  
“a b c d “C:\\user\\sub\_dir” “C:\\user\\other\_dir””  
[“a”, “b”, “c”, “d”, “C:usersub\_dir”, “C:userother\_dir”]

---

<div class="post-metadata">

**Author:** ![S-jb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@S-jb](https://www.ruby-forum.com/u/S-jb)\
**Post date:** [September 9, 2009, 3:15pm UTC](https://www.ruby-forum.com/t/shellwords-preserve-backslash/175135/3 "2009-09-09T15:15:55Z")

</div>

7stud – wrote:

> I don’t have a solution, but I see the same malfunction moving from ruby  
> 1.8.6 to ruby 1.9.1p243.

Is this something that should be reported as a bug? If so what is the  
appropriate way to do so?  
Thanks.
