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

[{'answer_id': 2280, 'author': 'Steve Willard', 'author_id': 402, 'author_profile': 'https://Stackoverflow.com/users/402', 'pm_score': 5, 'selected': False, 'text': '<p>I\'m definitely not a Ruby expert, but I\'ll give it a shot:</p>\n\n<pre><code>$ irb \nsystem "echo Hi"\nHi\n=&gt; true\n</code></pre>\n\n<p>You should also be able to do things like:</p>\n\n<pre><code>cmd = \'ls\'\nsystem(cmd)\n</code></pre>\n'}, {'answer_id': 2334, 'author': 'Rufo Sanchez', 'author_id': 140, 'author_profile': 'https://Stackoverflow.com/users/140', 'pm_score': 4, 'selected': False, 'text': "<p>You can also use the backtick operators (`), similar to Perl:</p>\n\n<pre><code>directoryListing = `ls /`\nputs directoryListing # prints the contents of the root directory\n</code></pre>\n\n<p>Handy if you need something simple.</p>\n\n<p>Which method you want to use depends on exactly what you're trying to accomplish; check the docs for more details about the different methods.</p>\n"}, {'answer_id': 2350, 'author': 'cynicalman', 'author_id': 410, 'author_profile': 'https://Stackoverflow.com/users/410', 'pm_score': 7, 'selected': False, 'text': "<p>The way I like to do this is using the <code>%x</code> literal, which makes it easy (and readable!) to use quotes in a command, like so:</p>\n\n<pre><code>directorylist = %x[find . -name '*test.rb' | sort]\n</code></pre>\n\n<p>Which, in this case, will populate file list with all test files under the current directory, which you can process as expected:</p>\n\n<pre><code>directorylist.each do |filename|\n  filename.chomp!\n  # work with file\nend\n</code></pre>\n"}, {'answer_id': 2400, 'author': 'Steve Willard', 'author_id': 402, 'author_profile': 'https://Stackoverflow.com/users/402', 'pm_score': 12, 'selected': True, 'text': '<p>This explanation is based on a commented <a href="http://gist.github.com/4069" rel="noreferrer">Ruby script</a> from a friend of mine. If you want to improve the script, feel free to update it at the link.</p>\n\n<p>First, note that when Ruby calls out to a shell, it typically calls <code>/bin/sh</code>, <em>not</em> Bash. Some Bash syntax is not supported by <code>/bin/sh</code> on all systems.</p>\n\n<p>Here are ways to execute a shell script:</p>\n\n<pre><code>cmd = "echo \'hi\'" # Sample string that can be used\n</code></pre>\n\n<ol>\n<li><p><code>Kernel#`</code> , commonly called backticks – <code>`cmd`</code></p>\n\n<p>This is like many other languages, including Bash, PHP, and Perl.</p>\n\n<p>Returns the result (i.e. standard output) of the shell command.</p>\n\n<p>Docs: <a href="http://ruby-doc.org/core/Kernel.html#method-i-60" rel="noreferrer">http://ruby-doc.org/core/Kernel.html#method-i-60</a></p>\n\n<pre><code>value = `echo \'hi\'`\nvalue = `#{cmd}`\n</code></pre></li>\n<li><p>Built-in syntax, <code>%x( cmd )</code></p>\n\n<p>Following the <code>x</code> character is a delimiter, which can be any character.\nIf the delimiter is one of the characters <code>(</code>, <code>[</code>, <code>{</code>, or <code>&lt;</code>,\nthe literal consists of the characters up to the matching closing delimiter,\ntaking account of nested delimiter pairs. For all other delimiters, the\nliteral comprises the characters up to the next occurrence of the\ndelimiter character.  String interpolation <code>#{ ... }</code> is allowed.</p>\n\n<p>Returns the result (i.e. standard output) of the shell command, just like the backticks.</p>\n\n<p>Docs: <a href="https://docs.ruby-lang.org/en/master/syntax/literals_rdoc.html#label-Percent+Strings" rel="noreferrer">https://docs.ruby-lang.org/en/master/syntax/literals_rdoc.html#label-Percent+Strings</a></p>\n\n<pre><code>value = %x( echo \'hi\' )\nvalue = %x[ #{cmd} ]\n</code></pre></li>\n<li><p><code>Kernel#system</code></p>\n\n<p>Executes the given command in a subshell. </p>\n\n<p>Returns <code>true</code> if the command was found and run successfully, <code>false</code> otherwise.</p>\n\n<p>Docs: <a href="http://ruby-doc.org/core/Kernel.html#method-i-system" rel="noreferrer">http://ruby-doc.org/core/Kernel.html#method-i-system</a></p>\n\n<pre><code>wasGood = system( "echo \'hi\'" )\nwasGood = system( cmd )\n</code></pre></li>\n<li><p><code>Kernel#exec</code></p>\n\n<p>Replaces the current process by running the given external command.</p>\n\n<p>Returns none, the current process is replaced and never continues.</p>\n\n<p>Docs: <a href="http://ruby-doc.org/core/Kernel.html#method-i-exec" rel="noreferrer">http://ruby-doc.org/core/Kernel.html#method-i-exec</a></p>\n\n<pre><code>exec( "echo \'hi\'" )\nexec( cmd ) # Note: this will never be reached because of the line above\n</code></pre></li>\n</ol>\n\n<p>Here\'s some extra advice:\n<code>$?</code>, which is the same as <code>$CHILD_STATUS</code>, accesses the status of the last system executed command if you use the backticks, <code>system()</code> or <code>%x{}</code>.\nYou can then access the <code>exitstatus</code> and <code>pid</code> properties:</p>\n\n<pre><code>$?.exitstatus\n</code></pre>\n\n<p>For more reading see:</p>\n\n<ul>\n<li><a href="http://www.elctech.com/blog/i-m-in-ur-commandline-executin-ma-commands" rel="noreferrer">http://www.elctech.com/blog/i-m-in-ur-commandline-executin-ma-commands</a></li>\n<li><a href="http://blog.jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html" rel="noreferrer">http://blog.jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html</a></li>\n<li><a href="http://tech.natemurray.com/2007/03/ruby-shell-commands.html" rel="noreferrer">http://tech.natemurray.com/2007/03/ruby-shell-commands.html</a></li>\n</ul>\n'}, {'answer_id': 4413, 'author': 'Nick Brosnahan', 'author_id': 528, 'author_profile': 'https://Stackoverflow.com/users/528', 'pm_score': 5, 'selected': False, 'text': '<p>Some things to think about when choosing between these mechanisms are:</p>\n\n<ol>\n<li>Do you just want stdout or do you\nneed stderr as well? Or even\nseparated out?</li>\n<li>How big is your output?  Do you want\nto hold the entire result in memory?</li>\n<li>Do you want to read some of your\noutput while the subprocess is still\nrunning?</li>\n<li>Do you need result codes?</li>\n<li>Do you need a Ruby object that\nrepresents the process and lets you\nkill it on demand?</li>\n</ol>\n\n<p>You may need anything from simple backticks (``), <code>system()</code>, and <code>IO.popen</code> to full-blown <code>Kernel.fork</code>/<code>Kernel.exec</code> with <code>IO.pipe</code> and <code>IO.select</code>.</p>\n\n<p>You may also want to throw timeouts into the mix if a sub-process takes too long to execute.</p>\n\n<p>Unfortunately, it very much <strong><em>depends</em></strong>.</p>\n'}, {'answer_id': 39228, 'author': 'Mihai A', 'author_id': 4212, 'author_profile': 'https://Stackoverflow.com/users/4212', 'pm_score': 6, 'selected': False, 'text': '<p>Here\'s the best article in my opinion about running shell scripts in Ruby: "<a href="http://tech.natemurray.com/2007/03/ruby-shell-commands.html" rel="noreferrer">6 Ways to Run Shell Commands in Ruby</a>".</p>\n\n<p>If you only need to get the output use backticks.</p>\n\n<p>I needed more advanced stuff like STDOUT and STDERR so I used the Open4 gem. You have all the methods explained there.</p>\n'}, {'answer_id': 94880, 'author': 'anshul', 'author_id': 17674, 'author_profile': 'https://Stackoverflow.com/users/17674', 'pm_score': 6, 'selected': False, 'text': '<p>My favourite is <a href="http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open3/rdoc/Open3.html" rel="noreferrer">Open3</a></p>\n\n<pre><code>  require "open3"\n\n  Open3.popen3(\'nroff -man\') { |stdin, stdout, stderr| ... }\n</code></pre>\n'}, {'answer_id': 3050311, 'author': 'j-g-faustus', 'author_id': 207663, 'author_profile': 'https://Stackoverflow.com/users/207663', 'pm_score': 5, 'selected': False, 'text': '<p>One more option:</p>\n\n<p>When you:</p>\n\n<ul>\n<li>need stderr as well as stdout </li>\n<li>can\'t/won\'t use Open3/Open4 (they throw exceptions in NetBeans on my Mac, no idea why)</li>\n</ul>\n\n<p>You can use shell redirection:</p>\n\n<pre><code>puts %x[cat bogus.txt].inspect\n  =&gt; ""\n\nputs %x[cat bogus.txt 2&gt;&amp;1].inspect\n  =&gt; "cat: bogus.txt: No such file or directory\\n"\n</code></pre>\n\n<p>The <code>2&gt;&amp;1</code> syntax works across <a href="http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html" rel="noreferrer">Linux</a>, Mac and <a href="http://support.microsoft.com/kb/110930" rel="noreferrer">Windows</a> since the early days of MS-DOS.</p>\n'}, {'answer_id': 9351735, 'author': 'nkm', 'author_id': 506559, 'author_profile': 'https://Stackoverflow.com/users/506559', 'pm_score': 4, 'selected': False, 'text': '<p>We can achieve it in multiple ways.</p>\n\n<p>Using <code>Kernel#exec</code>, nothing after this command is executed:</p>\n\n<pre><code>exec(\'ls ~\')\n</code></pre>\n\n<p>Using <code>backticks or %x</code></p>\n\n<pre><code>`ls ~`\n=&gt; "Applications\\nDesktop\\nDocuments"\n%x(ls ~)\n=&gt; "Applications\\nDesktop\\nDocuments"\n</code></pre>\n\n<p>Using <code>Kernel#system</code> command, returns <code>true</code> if successful, <code>false</code> if unsuccessful and returns <code>nil</code> if command execution fails:</p>\n\n<pre><code>system(\'ls ~\')\n=&gt; true\n</code></pre>\n'}, {'answer_id': 9370070, 'author': 'Ryan Tate', 'author_id': 112275, 'author_profile': 'https://Stackoverflow.com/users/112275', 'pm_score': 4, 'selected': False, 'text': '<p>Using the answers here and linked in Mihai\'s answer, I put together a function that meets these requirements:</p>\n\n<ol>\n<li>Neatly captures STDOUT and STDERR so they don\'t "leak" when my script is run from the console.</li>\n<li>Allows arguments to be passed to the shell as an array, so there\'s no need to worry about escaping.</li>\n<li>Captures the exit status of the command so it is clear when an error has occurred.</li>\n</ol>\n\n<p>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 <code>system</code>, which simply returns <code>true</code> in such cases. </p>\n\n<p>Code follows. The specific function is <code>system_quietly</code>:</p>\n\n<pre><code>require \'open3\'\n\nclass ShellError &lt; StandardError; end\n\n#actual function:\ndef system_quietly(*cmd)\n  exit_status=nil\n  err=nil\n  out=nil\n  Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thread|\n    err = stderr.gets(nil)\n    out = stdout.gets(nil)\n    [stdin, stdout, stderr].each{|stream| stream.send(\'close\')}\n    exit_status = wait_thread.value\n  end\n  if exit_status.to_i &gt; 0\n    err = err.chomp if err\n    raise ShellError, err\n  elsif out\n    return out.chomp\n  else\n    return true\n  end\nend\n\n#calling it:\nbegin\n  puts system_quietly(\'which\', \'ruby\')\nrescue ShellError\n  abort "Looks like you don\'t have the `ruby` command. Odd."\nend\n\n#output: =&gt; "/Users/me/.rvm/rubies/ruby-1.9.2-p136/bin/ruby"\n</code></pre>\n'}, {'answer_id': 16975172, 'author': 'Utensil', 'author_id': 200764, 'author_profile': 'https://Stackoverflow.com/users/200764', 'pm_score': 5, 'selected': False, 'text': '<p>The answers above are already quite great, but I really want to share the following summary article: "<a href="http://tech.natemurray.com/2007/03/ruby-shell-commands.html" rel="noreferrer">6 Ways to Run Shell Commands in Ruby</a>"</p>\n\n<p>Basically, it tells us:</p>\n\n<p><code>Kernel#exec</code>:</p>\n\n<pre><code>exec \'echo "hello $HOSTNAME"\'\n</code></pre>\n\n<p><code>system</code> and <code>$?</code>:</p>\n\n<pre><code>system \'false\' \nputs $?\n</code></pre>\n\n<p>Backticks (`):</p>\n\n<pre><code>today = `date`\n</code></pre>\n\n<p><code>IO#popen</code>:</p>\n\n<pre><code>IO.popen("date") { |f| puts f.gets }\n</code></pre>\n\n<p><code>Open3#popen3</code> -- stdlib:</p>\n\n<pre><code>require "open3"\nstdin, stdout, stderr = Open3.popen3(\'dc\') \n</code></pre>\n\n<p><code>Open4#popen4</code> -- a gem:</p>\n\n<pre><code>require "open4" \npid, stdin, stdout, stderr = Open4::popen4 "false" # =&gt; [26327, #&lt;IO:0x6dff24&gt;, #&lt;IO:0x6dfee8&gt;, #&lt;IO:0x6dfe84&gt;]\n</code></pre>\n'}, {'answer_id': 26369473, 'author': 'JayCrossler', 'author_id': 116793, 'author_profile': 'https://Stackoverflow.com/users/116793', 'pm_score': 0, 'selected': False, 'text': '<p>Here\'s a cool one that I use in a ruby script on OS X (so that I can start a script and get an update even after toggling away from the window):</p>\n\n<pre><code>cmd = %Q|osascript -e \'display notification "Server was reset" with title "Posted Update"\'|\nsystem ( cmd )\n</code></pre>\n'}, {'answer_id': 33525154, 'author': 'MonsieurDart', 'author_id': 368330, 'author_profile': 'https://Stackoverflow.com/users/368330', 'pm_score': 4, 'selected': False, 'text': '<p>Don\'t forget the <code>spawn</code> command to create a background process to execute the specified command. You can even wait for its completion using the <code>Process</code> class and the returned <code>pid</code>:</p>\n\n<pre><code>pid = spawn("tar xf ruby-2.0.0-p195.tar.bz2")\nProcess.wait pid\n\npid = spawn(RbConfig.ruby, "-eputs\'Hello, world!\'")\nProcess.wait pid\n</code></pre>\n\n<p>The doc says: This method is similar to <code>#system</code> but it doesn\'t wait for the command to finish.</p>\n'}, {'answer_id': 34226348, 'author': 'Kashyap', 'author_id': 496289, 'author_profile': 'https://Stackoverflow.com/users/496289', 'pm_score': 3, 'selected': False, 'text': '<p>If you have a more complex case than the common case that can not be handled with <code>``</code>, then check out  <a href="http://ruby-doc.org/core-1.9.3/Kernel.html#method-i-spawn" rel="nofollow noreferrer"><code>Kernel.spawn()</code></a>. This seems to be the most generic/full-featured provided by stock Ruby to execute external commands.</p>\n\n<p>You can use it to:</p>\n\n<ul>\n<li>create process groups (Windows).</li>\n<li>redirect in, out, error to files/each-other.</li>\n<li>set env vars, umask.</li>\n<li>change the directory before executing a command.</li>\n<li>set resource limits for CPU/data/etc.</li>\n<li>Do everything that can be done with other options in other answers, but with more code.</li>\n</ul>\n\n<p>The <a href="http://ruby-doc.org/core-1.9.3/Kernel.html#method-i-spawn" rel="nofollow noreferrer">Ruby documentation</a> has good enough examples:</p>\n\n<pre><code>env: hash\n  name =&gt; val : set the environment variable\n  name =&gt; nil : unset the environment variable\ncommand...:\n  commandline                 : command line string which is passed to the standard shell\n  cmdname, arg1, ...          : command name and one or more arguments (no shell)\n  [cmdname, argv0], arg1, ... : command name, argv[0] and zero or more arguments (no shell)\noptions: hash\n  clearing environment variables:\n    :unsetenv_others =&gt; true   : clear environment variables except specified by env\n    :unsetenv_others =&gt; false  : dont clear (default)\n  process group:\n    :pgroup =&gt; true or 0 : make a new process group\n    :pgroup =&gt; pgid      : join to specified process group\n    :pgroup =&gt; nil       : dont change the process group (default)\n  create new process group: Windows only\n    :new_pgroup =&gt; true  : the new process is the root process of a new process group\n    :new_pgroup =&gt; false : dont create a new process group (default)\n  resource limit: resourcename is core, cpu, data, etc.  See Process.setrlimit.\n    :rlimit_resourcename =&gt; limit\n    :rlimit_resourcename =&gt; [cur_limit, max_limit]\n  current directory:\n    :chdir =&gt; str\n  umask:\n    :umask =&gt; int\n  redirection:\n    key:\n      FD              : single file descriptor in child process\n      [FD, FD, ...]   : multiple file descriptor in child process\n    value:\n      FD                        : redirect to the file descriptor in parent process\n      string                    : redirect to file with open(string, "r" or "w")\n      [string]                  : redirect to file with open(string, File::RDONLY)\n      [string, open_mode]       : redirect to file with open(string, open_mode, 0644)\n      [string, open_mode, perm] : redirect to file with open(string, open_mode, perm)\n      [:child, FD]              : redirect to the redirected file descriptor\n      :close                    : close the file descriptor in child process\n    FD is one of follows\n      :in     : the file descriptor 0 which is the standard input\n      :out    : the file descriptor 1 which is the standard output\n      :err    : the file descriptor 2 which is the standard error\n      integer : the file descriptor of specified the integer\n      io      : the file descriptor specified as io.fileno\n  file descriptor inheritance: close non-redirected non-standard fds (3, 4, 5, ...) or not\n    :close_others =&gt; false : inherit fds (default for system and exec)\n    :close_others =&gt; true  : dont inherit (default for spawn and IO.popen)\n</code></pre>\n'}, {'answer_id': 37329716, 'author': 'Ian', 'author_id': 2063546, 'author_profile': 'https://Stackoverflow.com/users/2063546', 'pm_score': 9, 'selected': False, 'text': '<p>Here\'s a flowchart based on "<a href="https://stackoverflow.com/questions/7212573/when-to-use-each-method-of-launching-a-subprocess-in-ruby/7263556#7263556">When to use each method of launching a subprocess in Ruby</a>".  See also, "<a href="https://stackoverflow.com/questions/1401002/trick-an-application-into-thinking-its-stdout-is-a-terminal-not-a-pipe/1402389#1402389">Trick an application into thinking its stdout is a terminal, not a pipe</a>".</p>\n\n<p><a href="https://i.stack.imgur.com/1Vuvp.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1Vuvp.png" alt="enter image description here"></a></p>\n\n\n'}, {'answer_id': 42270536, 'author': 'Vaisakh VM', 'author_id': 1905008, 'author_profile': 'https://Stackoverflow.com/users/1905008', 'pm_score': 3, 'selected': False, 'text': "<p>The backticks (`) method is the easiest one to call shell commands from Ruby. It returns the result of the shell command:</p>\n\n<pre><code>     url_request = 'http://google.com'\n     result_of_shell_command = `curl #{url_request}`\n</code></pre>\n"}, {'answer_id': 43125428, 'author': 'Alex Lorsung', 'author_id': 5583376, 'author_profile': 'https://Stackoverflow.com/users/5583376', 'pm_score': 4, 'selected': False, 'text': '<p>The easiest way is, for example:</p>\n\n<pre><code>reboot = `init 6`\nputs reboot\n</code></pre>\n'}, {'answer_id': 44336764, 'author': 'dragon788', 'author_id': 3794873, 'author_profile': 'https://Stackoverflow.com/users/3794873', 'pm_score': 4, 'selected': False, 'text': '<p>If you really need Bash, per the note in the "best" answer.</p>\n\n<blockquote>\n  <p>First, note that when Ruby calls out to a shell, it typically calls <code>/bin/sh</code>, <em>not</em> Bash. Some Bash syntax is not supported by <code>/bin/sh</code> on all systems. </p>\n</blockquote>\n\n<p>If you need to use Bash, insert <code>bash -c "your Bash-only command"</code> inside of your desired calling method:</p>\n\n<pre><code>quick_output = system("ls -la")\n</code></pre>\n\n<pre><code>quick_bash = system("bash -c \'ls -la\'")\n</code></pre>\n\n<p>To test:</p>\n\n<pre><code>system("echo $SHELL")\nsystem(\'bash -c "echo $SHELL"\')\n</code></pre>\n\n<p>Or if you are running an existing script file like</p>\n\n<pre><code>script_output = system("./my_script.sh")\n</code></pre>\n\n<p>Ruby <em>should</em> honor the shebang, but you could always use </p>\n\n<pre><code>system("bash ./my_script.sh")\n</code></pre>\n\n<p>to make sure, though there may be a slight overhead from <code>/bin/sh</code> running <code>/bin/bash</code>, you probably won\'t notice.</p>\n'}, {'answer_id': 47880798, 'author': 'barlop', 'author_id': 385907, 'author_profile': 'https://Stackoverflow.com/users/385907', 'pm_score': 3, 'selected': False, 'text': '<p>Given a command like <code>attrib</code>:</p>\n\n<pre><code>require \'open3\'\n\na="attrib"\nOpen3.popen3(a) do |stdin, stdout, stderr|\n  puts stdout.read\nend\n</code></pre>\n\n<p>I\'ve found that while this method isn\'t as memorable as </p>\n\n<pre><code>system("thecommand")\n</code></pre>\n\n<p>or  </p>\n\n<pre><code>`thecommand`\n</code></pre>\n\n<p>in backticks,  a good thing about this method compared to other methods is \n backticks don\'t seem to let me <code>puts</code> the command I run/store the command I want to run in a variable, and <code>system("thecommand")</code> 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.</p>\n\n<p>See "<a href="https://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html" rel="noreferrer">Executing commands in ruby</a>" and <a href="http://ruby-doc.org/stdlib-2.4.1/libdoc/open3/rdoc/Open3.html" rel="noreferrer">Ruby\'s Open3 documentation</a>.</p>\n'}, {'answer_id': 51191872, 'author': 'lucaortis', 'author_id': 8709628, 'author_profile': 'https://Stackoverflow.com/users/8709628', 'pm_score': 2, 'selected': False, 'text': "<p>This is not really an answer but maybe someone will find it useful:</p>\n\n<p>When using TK GUI on Windows, and you need to call shell commands from rubyw, you will always have an annoying CMD window popping up for less then a second.</p>\n\n<p>To avoid this you can use:</p>\n\n<pre><code>WIN32OLE.new('Shell.Application').ShellExecute('ipconfig &gt; log.txt','','','open',0)\n</code></pre>\n\n<p>or </p>\n\n<pre><code>WIN32OLE.new('WScript.Shell').Run('ipconfig &gt; log.txt',0,0)\n</code></pre>\n\n<p>Both will store the <code>ipconfig</code> output inside <code>log.txt</code>, but no windows will come up.</p>\n\n<p>You will need to <code>require 'win32ole'</code> inside your script.</p>\n\n<p><code>system()</code>, <code>exec()</code> and <code>spawn()</code> will all pop up that annoying window when using TK and rubyw.</p>\n"}, {'answer_id': 70250573, 'author': 'Vasanth Saminathan', 'author_id': 5634603, 'author_profile': 'https://Stackoverflow.com/users/5634603', 'pm_score': 2, 'selected': False, 'text': "<p>Not sure about shell commands. I used following for capturing system command's output into a variable <strong>val</strong>:</p>\n<pre><code>val = capture(:stdout) do\n  system(&quot;pwd&quot;)\nend\n\nputs val\n</code></pre>\n<p>shortened version:</p>\n<pre><code>val = capture(:stdout) { system(&quot;pwd&quot;) }\n</code></pre>\n<p><strong>capture</strong> method is provided by <em>active_support/core_ext/kernel/reporting.rb</em></p>\n<p>Simlarly we can also capture standard errors too with <code>:stderr</code></p>\n"}, {'answer_id': 70596513, 'author': 'Teoman shipahi', 'author_id': 929902, 'author_profile': 'https://Stackoverflow.com/users/929902', 'pm_score': 0, 'selected': False, 'text': "<p>You can use <code>format</code> method as below to print some information:</p>\n<pre><code>puts format('%s', `ps`)\nputs format('%d MB', (`ps -o rss= -p #{Process.pid}`.to_i / 1024))\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/2232', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/25/']