Skip to content

Instantly share code, notes, and snippets.

View blevonius's full-sized avatar

Brian Levonius blevonius

  • SF Bay Area
  • 22:46 (UTC -07:00)
View GitHub Profile
@blevonius
blevonius / ruby_bash.rb
Last active January 15, 2016 21:34
call bash from ruby code
require 'posix/spawn' # https://github.com/rtomayko/posix-spawn
require 'shellwords'
# thanks to:
# http://blog.honeybadger.io/capturing-stdout-stderr-from-shell-commands-via-ruby/
# http://greyblake.com/blog/2013/09/21/how-to-call-bash-not-shell-from-ruby/
# https://github.com/rtomayko/posix-spawn
def bash(command)
escaped_command = Shellwords.escape(command)
pid, stdin, stdout, stderr = POSIX::Spawn.popen4("bash -c #{escaped_command}")