with - Calling shell commands from Ruby

ruby system() (14)

How do I call shell commands from inside of a Ruby program? How do I then get output from these commands back into Ruby?

Don't forget the spawn command to create a background process to execute the specified command. You can even wait for its completion using the Process class and the returned pid :

pid = spawn("tar xf ruby-2.0.0-p195.tar.bz2")
Process.wait pid

pid = spawn(RbConfig.ruby, "-eputs'Hello, world!'")
Process.wait pid

The doc says: This method is similar to #system but it doesn't wait for the command to finish.

Given a command eg attrib

require 'open3'

Open3.popen3(a) do |stdin, stdout, stderr|
  puts stdout.read

I've found that while this method isn't as memorable as e.g. system("thecommand") or thecommand in backticks, a good thing about this method compared to other methods.. is e.g. backticks doesn't seem to let me 'puts' the command I run / store the command I want to run in a variable, and system("thecommand") doesn't seem to let me get the output. Whereas this method lets me do both of those things, and it lets me access stdin, stdout and stderr independently.



Here's the best article in my opinion about running shell scripts in Ruby: " http://tech.natemurray.com/2007/03/ruby-shell-commands.html ".

If you only need to get the output use backticks.

I needed more advanced stuff like STDOUT and STDERR so I used the Open4 gem. You have all the methods explained there.

If you have a more complex case than the common case (that can not be handled with `` ) then check out Kernel.spawn() here . This seems to be the most generic/full-featured provided by stock Ruby to execute external commands.

E.g. you can use it to:

  • create process groups (Windows)
  • redirect in, out, error to files/each-other.
  • set env vars, umask
  • change dir before executing command
  • set resource limits for CPU/data/...
  • Do everything that can be done with other options in other answers, but with more code.

Official here has good enough examples.

env: hash
  name => val : set the environment variable
  name => nil : unset the environment variable
  commandline                 : command line string which is passed to the standard shell
  cmdname, arg1, ...          : command name and one or more arguments (no shell)
  [cmdname, argv0], arg1, ... : command name, argv[0] and zero or more arguments (no shell)
options: hash
  clearing environment variables:
    :unsetenv_others => true   : clear environment variables except specified by env
    :unsetenv_others => false  : dont clear (default)
  process group:
    :pgroup => true or 0 : make a new process group
    :pgroup => pgid      : join to specified process group
    :pgroup => nil       : dont change the process group (default)
  create new process group: Windows only
    :new_pgroup => true  : the new process is the root process of a new process group
    :new_pgroup => false : dont create a new process group (default)
  resource limit: resourcename is core, cpu, data, etc.  See Process.setrlimit.
    :rlimit_resourcename => limit
    :rlimit_resourcename => [cur_limit, max_limit]
  current directory:
    :chdir => str
    :umask => int
      FD              : single file descriptor in child process
      [FD, FD, ...]   : multiple file descriptor in child process
      FD                        : redirect to the file descriptor in parent process
      string                    : redirect to file with open(string, "r" or "w")
      [string]                  : redirect to file with open(string, File::RDONLY)
      [string, open_mode]       : redirect to file with open(string, open_mode, 0644)
      [string, open_mode, perm] : redirect to file with open(string, open_mode, perm)
      [:child, FD]              : redirect to the redirected file descriptor
      :close                    : close the file descriptor in child process
    FD is one of follows
      :in     : the file descriptor 0 which is the standard input
      :out    : the file descriptor 1 which is the standard output
      :err    : the file descriptor 2 which is the standard error
      integer : the file descriptor of specified the integer
      io      : the file descriptor specified as io.fileno
  file descriptor inheritance: close non-redirected non-standard fds (3, 4, 5, ...) or not
    :close_others => false : inherit fds (default for system and exec)
    :close_others => true  : dont inherit (default for spawn and IO.popen)

If you really need Bash, per the note in the "best" answer.

First, note that when Ruby calls out to a shell, it typically calls /bin/sh , not Bash. Some Bash syntax is not supported by /bin/sh on all systems.

If you need to use Bash, insert bash -c "your Bash-only command" inside of your desired calling method.

quick_output = system("ls -la")

quick_bash = system("bash -c 'ls -la'")

To test:

system("echo $SHELL") system('bash -c "echo $SHELL"')

Or if you are running an existing script file (eg script_output = system("./my_script.sh") ) Ruby should honor the shebang, but you could always use system("bash ./my_script.sh") to make sure (though there may be a slight overhead from /bin/sh running /bin/bash , you probably won't notice.

Not really an answer but maybe someone will find this useful, and its regarding to this.

When using TK GUI on Windows, and u need to call shell commands from rubyw, u will always have an annoying cmd window popping up for less then a sec.

To avoid this u can use

WIN32OLE.new('Shell.Application').ShellExecute('ipconfig > log.txt','','','open',0)


WIN32OLE.new('WScript.Shell').Run('ipconfig > log.txt',0,0)

Both will store ipconfig's output inside 'log.txt', but no windows will come up.

U will need to require 'win32ole' inside your script.

system() , exec() and spawn() will all pop up that annoying window when using TK and rubyw.

One more option:

When you:

  • need stderr as well as stdout
  • can't/won't use Open3/Open4 (they throw exceptions in NetBeans on my Mac, no idea why)

You can use shell redirection:

puts %x[cat bogus.txt].inspect
  => ""

puts %x[cat bogus.txt 2>&1].inspect
  => "cat: bogus.txt: No such file or directory\n"

The 2>&1 syntax works across Linux , Mac and Windows since the early days of MS-DOS.

The answers above are already quite great, but I really want to share the following summary article: " http://tech.natemurray.com/2007/03/ruby-shell-commands.html "

Basically, it tells us:

Kernel#exec :

exec 'echo "hello $HOSTNAME"'

system and $? :

system 'false' 
puts $?

Backticks (`):

today = `date`

IO#popen :

IO.popen("date") { |f| puts f.gets }

Open3#popen3 -- stdlib:

require "open3"
stdin, stdout, stderr = Open3.popen3('dc') 

Open4#popen4 -- a gem:

require "open4" 
pid, stdin, stdout, stderr = Open4::popen4 "false" # => [26327, #<IO:0x6dff24>, #<IO:0x6dfee8>, #<IO:0x6dfe84>]

The way I like to do this is using the %x literal, which makes it easy (and readable!) to use quotes in a command, like so:

directorylist = %x[find . -name '*test.rb' | sort]

Which, in this case, will populate file list with all test files under the current directory, which you can process as expected:

directorylist.each do |filename|
  # work with file

Using the answers here and linked in Mihai's answer, I put together a function that meets these requirements:

  1. Neatly captures STDOUT and STDERR so they don't "leak" when my script is run from the console.
  2. Allows arguments to be passed to the shell as an array, so there's no need to worry about escaping.
  3. Captures the exit status of the command so it is clear when an error has occurred.

As a bonus, this one will also return STDOUT in cases where the shell command exits successfully (0) and puts anything on STDOUT. In this manner, it differs from system , which simply returns true in such cases.

Code follows. The specific function is system_quietly :

require 'open3'

class ShellError < StandardError; end

#actual function:
def system_quietly(*cmd)
  Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thread|
    err = stderr.gets(nil)
    out = stdout.gets(nil)
    [stdin, stdout, stderr].each{|stream| stream.send('close')}
    exit_status = wait_thread.value
  if exit_status.to_i > 0
    err = err.chomp if err
    raise ShellError, err
  elsif out
    return out.chomp
    return true

#calling it:
  puts system_quietly('which', 'ruby')
rescue ShellError
  abort "Looks like you don't have the `ruby` command. Odd."

#output: => "/Users/me/.rvm/rubies/ruby-1.9.2-p136/bin/ruby"

We can achieve it in multiple ways.

Using Kernel#exec , nothing after this command is executed:

exec('ls ~')

Using backticks or %x

`ls ~`
=> "Applications\nDesktop\nDocuments"
%x(ls ~)
=> "Applications\nDesktop\nDocuments"

Using Kernel#system command, returns true if successful, false if unsuccessful and returns nil if command execution fails:

system('ls ~')
=> true

easiest way is, for example:

reboot = `init 6`
puts reboot

  • backticks ` method is the easiest one to call shell commands from ruby. It returns the result of the shell command.

     url_request = 'http://google.com'
     result_of_shell_command = `curl #{url_request}`