# How to redirect a "system" standard output and standard error to a variable (Linux)

**URL:** <https://www.ruby-forum.com/t/how-to-redirect-a-system-standard-output-and-standard-error-to-a-variable-linux/121429>\
**Category:** Ruby\
**Created:** [November 27, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/how-to-redirect-a-system-standard-output-and-standard-error-to-a-variable-linux/121429 "2007-11-27T22:18:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Venks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/edb3f5/32.png) [@Venks](https://www.ruby-forum.com/u/Venks)\
**Post date:** [November 27, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/how-to-redirect-a-system-standard-output-and-standard-error-to-a-variable-linux/121429/1 "2007-11-27T22:18:36Z")

</div>

To clarify further I want to capture any standard output or standard  
error.

Thanks,

---------- Forwarded message ----------  
From: Venks [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Nov 27, 2007 3:58 PM  
Subject: How to redirect a “system” standard output to a variable  
To: removed\_email\_address@domain.invalid

Hi,

I need to redirect any standard output thrown when using a system call  
into  
a Ruby variable. I tried to search for a solution but couldn’t find  
anything  
that worked. May be I am not using the right search terms.

Here is what I am trying to do.

begin

`rm somefile`

end

If “somefile” doesn’t exist the OS throws the message “rm: cannot remove  
`somefile’: No such file or directory” which I want to capture into a  
variable.

Thanks,

---

<div class="post-metadata">

**Author:** ![Venks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/edb3f5/32.png) [@Venks](https://www.ruby-forum.com/u/Venks)\
**Post date:** [November 27, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/how-to-redirect-a-system-standard-output-and-standard-error-to-a-variable-linux/121429/2 "2007-11-27T22:42:03Z")

</div>

Venks wrote:

> To clarify further I want to capture any standard output or standard  
> error.

irb(main):001:0\> r = `rm somefile 2>&1`  
=\> “rm: cannot remove `somefile’: No such file or directory\n”

ilan

---

<div class="post-metadata">

**Author:** ![Venks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/edb3f5/32.png) [@Venks](https://www.ruby-forum.com/u/Venks)\
**Post date:** [November 27, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/how-to-redirect-a-system-standard-output-and-standard-error-to-a-variable-linux/121429/3 "2007-11-27T22:52:34Z")

</div>

On Nov 27, 2:17 pm, Venks [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Note: parts of this message were removed by the gateway to make it a legal Usenet post.
> 
> To clarify further I want to capture any standard output or standard error.

Google “ruby popen3” and “ruby popen4”

---

<div class="post-metadata">

**Author:** ![Venks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/edb3f5/32.png) [@Venks](https://www.ruby-forum.com/u/Venks)\
**Post date:** [November 28, 2007, 2:00am UTC](https://www.ruby-forum.com/t/how-to-redirect-a-system-standard-output-and-standard-error-to-a-variable-linux/121429/4 "2007-11-28T02:00:57Z")

</div>

On Nov 27, 1:55 pm, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > To clarify further I want to capture any standard output or standard error.

I was dealing with this myself today. Here is the solution came  
across which works for me. I’m combining the stderr with stdout from  
the system call. You can remove the exitcode stuff if you dont need  
it.

IO.popen(“ls nofile 2\>&1”) {|f|  
output = f.read  
exitcode = Process.waitpid2(f.pid)[1] \>\> 8  
puts “this is the output”  
puts output  
puts “this is the exit code”  
puts exitcode  
}

#./test.rb  
this is the output  
nofile: No such file or directory  
this is the exit code  
2

HTH. G.

---

<div class="post-metadata">

**Author:** ![Venks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/edb3f5/32.png) [@Venks](https://www.ruby-forum.com/u/Venks)\
**Post date:** [December 6, 2007, 1:40am UTC](https://www.ruby-forum.com/t/how-to-redirect-a-system-standard-output-and-standard-error-to-a-variable-linux/121429/5 "2007-12-06T01:40:48Z")

</div>

On Tue, 27 Nov 2007 16:17:30 -0500, Venks wrote:

> Subject: How to redirect a “system” standard output to a variable To:
> 
> Thanks,  
> You need to use popen3, because the message is printed on standard  
> error,  
> not standard out.

–Ken

---

<div class="post-metadata">

**Author:** ![Venks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/edb3f5/32.png) [@Venks](https://www.ruby-forum.com/u/Venks)\
**Post date:** [November 27, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/how-to-redirect-a-system-standard-output-and-standard-error-to-a-variable-linux/121429/6 "2007-11-27T22:55:45Z")

</div>

> To clarify further I want to capture any standard output or standard error.

I think the shortest and cleanest is

require ‘systemu’

🙂

It is a bit similar to the popen3 one i think but easier IMHO
