# Redirecting $stdout and $stderr streams

**URL:** <https://www.ruby-forum.com/t/redirecting-stdout-and-stderr-streams/223437>\
**Category:** Ruby\
**Created:** [October 4, 2012, 2:52am UTC](https://www.ruby-forum.com/t/redirecting-stdout-and-stderr-streams/223437 "2012-10-04T02:52:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [October 4, 2012, 2:52am UTC](https://www.ruby-forum.com/t/redirecting-stdout-and-stderr-streams/223437/1 "2012-10-04T02:52:59Z")

</div>

Guys, how are you?  
I was googling about “redirecting $stdout and $stderr” and founded  
this:

$stdout.puts “stdout”  
$stderr.puts “stderr”

Then in the comand line you invoke the above snippet, lets suppose that  
is called test.rb

ruby test.rb \> stdout 2\> stderr

What that makes is create 2 files in the current directory, one called  
stdout and the other stderr, both have writed what’s supposed to be in  
each one. This work, but I’ve never seen that kind of “commands options”  
or “commands arguments” and neither appears in the commons reference  
ruby books, anybody knows what “\>” and the rest means when invoking  
test.rb?

Thanks.

---

<div class="post-metadata">

**Author:** ![DamiSSn\_MSG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@DamiSSn\_MSG](https://www.ruby-forum.com/u/DamiSSn_MSG)\
**Post date:** [October 4, 2012, 4:21am UTC](https://www.ruby-forum.com/t/redirecting-stdout-and-stderr-streams/223437/2 "2012-10-04T04:21:13Z")

</div>

On 3 Oct 2012, at 18:53, “Damián M. González” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Guys, how are you?

Reasonably well, thanks

anybody knows what “\>” and the rest means when invoking

> test.rb?

This is directives to the shell, not command arguments. The order of  
operations is:

1. The shell looks at a command line and performs interpretation.
2. The program gets the result of that interpretation as arguments.

This is specifically IO redirection. You can test by doing something  
like:

ls -R \> outfile.txt

Or on windows:

dir \> outfile.txt

You will see that though the ls command does not know how to make an  
outfile.txt, one is made anyway. This is handled by the shell, and the  
OS itself.

[http://pcsupport.about.com/od/commandlinereference/a/redirect-command-output-to-file.htm](http://pcsupport.about.com/od/commandlinereference/a/redirect-command-output-to-file.htm)

That link isn’t a bad intro to the same ideas

Jams

---

<div class="post-metadata">

**Author:** ![DamiSSn\_MSG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@DamiSSn\_MSG](https://www.ruby-forum.com/u/DamiSSn_MSG)\
**Post date:** [October 5, 2012, 3:50am UTC](https://www.ruby-forum.com/t/redirecting-stdout-and-stderr-streams/223437/3 "2012-10-05T03:50:51Z")

</div>

Thank you Jam and Joel.

Jam, well explained. The web page that you gives is very helpfull. I  
suppose that 2\> reference the stderr, since is a standard that the file  
descriptor of stderr is 2 in every program.

Joel, I will consider your example to implement it in some way.

See you around 🙂

---

<div class="post-metadata">

**Author:** ![DamiSSn\_MSG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@DamiSSn\_MSG](https://www.ruby-forum.com/u/DamiSSn_MSG)\
**Post date:** [October 4, 2012, 2:54pm UTC](https://www.ruby-forum.com/t/redirecting-stdout-and-stderr-streams/223437/4 "2012-10-04T14:54:15Z")

</div>

I don’t know where you’re headed with this, but this is what I use to  
create a logfile when I’m not running from a console:

begin

#Output log  
orig\_std\_out = STDOUT.clone #Make a record of the default console output  
to return to it later  
$stdout.reopen(“Rubylog.txt”, “w”) #Create / overwrite logfile  
$stdout.sync = true #Allow interception of console output  
$stderr.reopen($stdout) #Pass errorserrors to logfile

#Do stuff…  
puts “I’m doing stuff!”

rescue

puts %Q|Fatal error occurred:\n#{$!}\n\nDebugging  
Information:\n#{$@}|,“Error” #Logfile output

ensure

STDOUT.reopen(orig\_std\_out) #Restore output to console

end
