# IO redirection

**URL:** <https://www.ruby-forum.com/t/io-redirection/126105>\
**Category:** Ruby\
**Created:** [January 15, 2008, 1:25pm UTC](https://www.ruby-forum.com/t/io-redirection/126105 "2008-01-15T13:25:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [January 15, 2008, 1:25pm UTC](https://www.ruby-forum.com/t/io-redirection/126105/1 "2008-01-15T13:25:02Z")

</div>

Basically, I need to redirect the results of exec. While I can  
$stdout.reopen(file), and I can redefine $stdout to anything with a  
`write' method, I seem to be unable to send the results of exec to anything within my script (so exec('echo','1') would send "1\n" to a method). I tried to subclass IO, but then I get :in`reopen’: uninitialized stream (IOError)

Any ideas?

Daniel Brumbaugh K.

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [January 15, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/io-redirection/126105/2 "2008-01-15T16:07:55Z")

</div>

Hi,

Use

p exec(‘echo’, ‘1’)

instead of  
exec(‘echo’, ‘1’)

And you will get ‘1\n’ in the specified file.

Thank you.
