# Redirect stdout

**URL:** <https://rubytalk.org/t/redirect-stdout/9024>\
**Category:** ruby-talk\
**Created:** [24 November 2003 12:48 UTC](https://rubytalk.org/t/redirect-stdout/9024 "2003-11-24T12:48:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [24 November 2003 12:48 UTC](https://rubytalk.org/t/redirect-stdout/9024/1 "2003-11-24T12:48:12Z")

</div>

I need to temporarily redirect standard output to nowhere. Not sure how to do.

Suggestions?

Thanks,  
-t0

---

<div class="post-metadata">

**Author:** ![Reimer\_Behrends](https://avatars.discourse-cdn.com/v4/letter/r/e36b37/32.png) [@Reimer\_Behrends](https://rubytalk.org/u/Reimer_Behrends)\
**Post date:** [24 November 2003 15:52 UTC](https://rubytalk.org/t/redirect-stdout/9024/2 "2003-11-24T15:52:12Z")

</div>

Assuming you run some \*IX:

```
STDOUT.reopen "/dev/null", "w"

```

will redirect the standard output (including not just Ruby output,  
but even the output of subprocesses) to /dev/null.

```
		Reimer Behrends

```

> **···**
>
> T. Onoma ([transami@runbox.com](mailto:transami@runbox.com)) wrote:
> 
> > I need to temporarily redirect standard output to nowhere. Not sure  
> > how to do.

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [24 November 2003 16:42 UTC](https://rubytalk.org/t/redirect-stdout/9024/3 "2003-11-24T16:42:10Z")

</div>

But it also loses the original STDOUT, and M Onoma did say that the  
redirection was to be temporary. You can accomplish that by saving  
the original value and restoring it later, like this:

```
old_stdout = STDOUT.dup
STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
. . .
STDOUT.reopen(old_stdout)

```

Or, if you aren’t invoking any processes external to ruby, you can leave  
STDOUT alone and just change $stdout:

```
$stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
. . .
$stdout = STDOUT

```

-Mark

> **···**
>
> On Mon, Nov 24, 2003 at 03:49:21PM +0000, Reimer Behrends wrote:
> 
> > T. Onoma ([transami@runbox.com](mailto:transami@runbox.com)) wrote:
> > 
> > > I need to temporarily redirect standard output to nowhere. Not sure  
> > > how to do.
> > 
> > Assuming you run some \*IX:
> > 
> > STDOUT.reopen “/dev/null”, “w”
> > 
> > will redirect the standard output (including not just Ruby output,  
> > but even the output of subprocesses) to /dev/null.

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [24 November 2003 16:42 UTC](https://rubytalk.org/t/redirect-stdout/9024/4 "2003-11-24T16:42:17Z")

</div>

Minor correction: forgot the mode on my open calls.  
Add , “w” to both of the below:

> STDOUT.reopen( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null” )  
> STDOUT.reopen( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null”, “w” )

> $stdout = File.open( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null” )  
> $stdout = File.open( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null”, “w” )

-Mark

> **···**
>
> On Mon, Nov 24, 2003 at 04:37:13PM +0000, Mark J. Reed wrote:

---

<div class="post-metadata">

**Author:** ![Eric\_Schwartz6](https://avatars.discourse-cdn.com/v4/letter/e/59ef9b/32.png) [@Eric\_Schwartz6](https://rubytalk.org/u/Eric_Schwartz6)\
**Post date:** [24 November 2003 19:32 UTC](https://rubytalk.org/t/redirect-stdout/9024/5 "2003-11-24T19:32:13Z")

</div>

“Mark J. Reed” [markjreed@mail.com](mailto:markjreed@mail.com) writes:

> Or, if you aren’t invoking any processes external to ruby, you can leave  
> STDOUT alone and just change $stdout:

Okay, this hints at the answer to a question I’ve been wondering about  
for a while now: what’s the difference between STDOUT/STDERR/STDIN and  
$stdout/$stderr/$stdin?

It seems your message implies that STDOUT/STDERR/STDIN are IO objects  
that when changed, the changes are passed on to children, but if you  
change $stdout and friends, those changes are not passed on to  
children.

If so, this confuses me greatly, since $stdout prints to stdout, and  
if open filehandles (including the magic 3) are inherited by children  
(as they are on most \*nices), then how can I reopen $stdout without  
changing stdout? Why is there such a (to me) confusing and needless  
distinction?

Of course, if I inferred incorrectly, disregard the previous  
paragraph. 🙂

-=Eric

> **···**
>
> –  
> Come to think of it, there are already a million monkeys on a million  
> typewriters, and Usenet is NOTHING like Shakespeare.  
> – Blair Houghton.

---

<div class="post-metadata">

**Author:** ![Reimer\_Behrends](https://avatars.discourse-cdn.com/v4/letter/r/e36b37/32.png) [@Reimer\_Behrends](https://rubytalk.org/u/Reimer_Behrends)\
**Post date:** [26 November 2003 00:37 UTC](https://rubytalk.org/t/redirect-stdout/9024/6 "2003-11-26T00:37:11Z")

</div>

[…]

> But it also loses the original STDOUT, and M Onoma did say that the  
> redirection was to be temporary.

Good catch. I missed that one.

> You can accomplish that by saving  
> the original value and restoring it later, like this:
> 
> old\_stdout = STDOUT.dup  
> STDOUT.reopen( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null” )  
> . . .  
> STDOUT.reopen(old\_stdout)

You need

```
old_stdout.close

```

here. Otherwise you have a file descriptor leak. Similarly:

> Or, if you aren’t invoking any processes external to ruby, you can leave  
> STDOUT alone and just change $stdout:
> 
> $stdout = File.open( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null” )  
> . . .  
> $stdout.close  
> $stdout = STDOUT

(And, of course, add the “w” flag to the open call, as you had already  
mentioned.)

```
	Reimer Behrends

```

> **···**
>
> Mark J. Reed ([markjreed@mail.com](mailto:markjreed@mail.com)) wrote:

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [24 November 2003 17:36 UTC](https://rubytalk.org/t/redirect-stdout/9024/7 "2003-11-24T17:36:33Z")

</div>

Perfecto!

You are an astute reader, Mark. 🙂

Thank you,  
-T0

> **···**
>
> On Monday 24 November 2003 05:42 pm, Mark J. Reed wrote:
> 
> > Minor correction: forgot the mode on my open calls.  
> > Add , “w” to both of the below:
> > 
> > On Mon, Nov 24, 2003 at 04:37:13PM +0000, Mark J. Reed wrote:
> > 
> > > ```
> > > STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
> > > 
> > > ```
> > 
> > STDOUT.reopen( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null”, “w” )
> > 
> > > ```
> > > $stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
> > > 
> > > ```
> > 
> > $stdout = File.open( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null”, “w” )
> > 
> > -Mark

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [26 November 2003 15:12 UTC](https://rubytalk.org/t/redirect-stdout/9024/8 "2003-11-26T15:12:32Z")

</div>

Right back at you for catching the fd leaks in my examples. Whups. 🙂

Thanks.

-Mark

> **···**
>
> On Wed, Nov 26, 2003 at 12:32:02AM +0000, Reimer Behrends wrote:
> 
> > Mark J. Reed ([markjreed@mail.com](mailto:markjreed@mail.com)) wrote:  
> > […]
> > 
> > > But it also loses the original STDOUT, and M Onoma did say that the  
> > > redirection was to be temporary.
> > 
> > Good catch. I missed that one.

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [28 November 2003 07:31 UTC](https://rubytalk.org/t/redirect-stdout/9024/9 "2003-11-28T07:31:06Z")

</div>

Thanks!

-t0

> **···**
>
> On Monday 24 November 2003 05:42 pm, Mark J. Reed wrote:
> 
> > Minor correction: forgot the mode on my open calls.  
> > Add , “w” to both of the below:
> > 
> > On Mon, Nov 24, 2003 at 04:37:13PM +0000, Mark J. Reed wrote:
> > 
> > > ```
> > > STDOUT.reopen( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
> > > 
> > > ```
> > 
> > STDOUT.reopen( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null”, “w” )
> > 
> > > ```
> > > $stdout = File.open( PLATFORM =~ /mswin/ ? "NUL" : "/dev/null" )
> > > 
> > > ```
> > 
> > $stdout = File.open( PLATFORM =~ /mswin/ ? “NUL” : “/dev/null”, “w” )
> > 
> > -Mark
