# Replacing single quotes and backslashes in strings

**URL:** <https://www.ruby-forum.com/t/replacing-single-quotes-and-backslashes-in-strings/50711>\
**Category:** Ruby\
**Created:** [December 20, 2005, 10:17am UTC](https://www.ruby-forum.com/t/replacing-single-quotes-and-backslashes-in-strings/50711 "2005-12-20T10:17:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 20, 2005, 10:17am UTC](https://www.ruby-forum.com/t/replacing-single-quotes-and-backslashes-in-strings/50711/1 "2005-12-20T10:17:44Z")

</div>

People,

I have been trying to find a solution to this on the net but with no  
luck:

I want to get a list of file names from a dir and pass the result to a  
du command eg:

```
puts `du file_name`

```

but if the filename has single quotes or backslashes in it eg:

```
puts `du phil'sfile_name` or: puts `du phil\sfile_name`

```

I have replace them with eg:

```
puts `du phil\'sfile_name` or: puts `du phil\\sfile_name`

```

I have tried all sorts of combinations with gsub with no success . .

Is there a solution with gsub or something else?

Thanks,

## Phil.

Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)  
GPO Box 3411  
Sydney NSW 2001  
Australia  
Mobile: +61:(0)411-185-652  
Fax: +61:(0)2-8221-9599  
E-mail: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 20, 2005, 11:41am UTC](https://www.ruby-forum.com/t/replacing-single-quotes-and-backslashes-in-strings/50711/2 "2005-12-20T11:41:55Z")

</div>

Logan,

On Tue, 2005-12-20 at 19:02 +0900, Logan C. wrote:

> > ```
> > puts `du file_name`
> > 
> > ```
> > 
> > Sydney NSW 2001  
> > Australia  
> > Mobile: +61:(0)411-185-652  
> > Fax: +61:(0)2-8221-9599  
> > E-mail: removed\_email\_address@domain.invalid
> 
> str = %q{du phil’sfile\_name}  
> puts `#{str.gsub(/['\\]/) { |meta_char| case meta_char; when "\\"; "\\ \\"; when "'"; "\\'"; end }}`

Whew!

Why doesn’t this work?:

line = line.gsub( /’/ ‘, "’" )  
line = line.gsub( ‘\’, “\\” )  
puts `du #{line}`

Thanks,

## Phil.

Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)  
GPO Box 3411  
Sydney NSW 2001  
Australia  
Mobile: +61:(0)411-185-652  
Fax: +61:(0)2-8221-9599  
E-mail: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 20, 2005, 11:05am UTC](https://www.ruby-forum.com/t/replacing-single-quotes-and-backslashes-in-strings/50711/3 "2005-12-20T11:05:49Z")

</div>

On Dec 20, 2005, at 4:15 AM, Phil R. wrote:

> but if the filename has single quotes or backslashes in it eg:
> 
> Mobile: +61:(0)411-185-652  
> Fax: +61:(0)2-8221-9599  
> E-mail: removed\_email\_address@domain.invalid

str = %q{du phil’sfile\_name}  
puts `#{str.gsub(/['\\]/) { |meta_char| case meta_char; when "\\"; "\\ \\"; when "'"; "\\'"; end }}`

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 20, 2005, 12:27pm UTC](https://www.ruby-forum.com/t/replacing-single-quotes-and-backslashes-in-strings/50711/4 "2005-12-20T12:27:03Z")

</div>

On Dec 20, 2005, at 5:41 AM, Philip R. wrote:

> > > I want to get a list of file names from a dir and pass the result  
> > > puts `du phil\'sfile_name` or: puts `du phil\\sfile_name`
> > 
> > puts `#{str.gsub(/[’\]/) { |meta\_char| case meta\_char; when “\”;  
> > line = line.gsub( ‘\’, “\\” )  
> > GPO Box 3411  
> > Sydney NSW 2001  
> > Australia  
> > Mobile: +61:(0)411-185-652  
> > Fax: +61:(0)2-8221-9599  
> > E-mail: removed\_email\_address@domain.invalid

Well, since `` invokes your shell you have to sort of “double-escape”  
the meta characters of single quotes. For instance with single quotes  
if you escape it once just for ruby, its not escaped for the shell so  
it may try to quote with single quotes instead of considering the  
single quote part of the file name. line.gsub(’\’, “\\”) should  
work but you have to be a little more careful with quotes. eg.  
line.gsub(/’/, “\’”). My code was meant to be extra-smart about the  
escaping. You don’t want to “over-escape” either.
