# Escapeshellcmd() for Ruby

**URL:** <https://www.ruby-forum.com/t/escapeshellcmd-for-ruby/91678>\
**Category:** Ruby\
**Created:** [April 11, 2007, 2:20pm UTC](https://www.ruby-forum.com/t/escapeshellcmd-for-ruby/91678 "2007-04-11T14:20:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hawe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hawe](https://www.ruby-forum.com/u/hawe)\
**Post date:** [April 11, 2007, 2:20pm UTC](https://www.ruby-forum.com/t/escapeshellcmd-for-ruby/91678/1 "2007-04-11T14:20:02Z")

</div>

Hi!

Is there something like PHP’s escapeshellcmd() function which removes  
special signs for system calls? Or do I have to do it myself and escape  
|;\>"? What else?

Thanks a lot,  
hawe

---

<div class="post-metadata">

**Author:** ![hawe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hawe](https://www.ruby-forum.com/u/hawe)\
**Post date:** [April 11, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/escapeshellcmd-for-ruby/91678/2 "2007-04-11T14:33:50Z")

</div>

On Apr 11, 2007, at 7:20 AM, hawe wrote:

> Is there something like PHP’s escapeshellcmd() function which removes  
> special signs for system calls? Or do I have to do it myself and  
> escape  
> |;\>"? What else?

I’m not aware of anything, but TextMate uses to following code for  
shell escaping:

# escape text to make it useable in a shell script as one

“word” (string)  
def e\_sh(str)  
str.to\_s.gsub(/(?=[^a-zA-Z0-9\_./-\x7F-\xFF\n])/, ‘\’).gsub(/\n/,  
“’\n’”).sub(/^$/, “’’”)  
end

Not sure how portable that is, but hopefully it will get you started.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![hawe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hawe](https://www.ruby-forum.com/u/hawe)\
**Post date:** [April 11, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/escapeshellcmd-for-ruby/91678/3 "2007-04-11T16:53:09Z")

</div>

On Wed, Apr 11, 2007 at 09:20:05PM +0900, hawe wrote:

> Is there something like PHP’s escapeshellcmd() function which removes  
> special signs for system calls? Or do I have to do it myself and escape  
> |;\>"? What else?

You can use system("/usr/bin/foo",“bar”,“baz”). This runs command  
“/usr/bin/foo” and passes it arguments “bar” and “baz”, without going  
through a shell at all - so shell escaping isn’t required.

irb(main):002:0\> system("/bin/echo",“hello”,“2\>/dev/null”,“world”)  
hello 2\>/dev/null world  
=\> true
