TIP 202: Add 2>@1 Special Case to [open] and [exec]

Login
Author:		Jeff Hobbs <[email protected]>
State:		Final
Type:		Project
Tcl-Version:	8.5
Vote:		Done
Created:	19-May-2004
Post-History:	
Keywords:	Tcl, redirection
Tcl-Ticket:	957132

Abstract

This TIP proposes adding a new redirection case '2>@1' that redirects the error channel to the output channel in open and exec. This would only be valid at the end of a pipeline.

Rationale

Tcl is currently limited on how stderr can be managed from a command opened with open | or exec. This TIP adds the ability to simply combine the stderr stream with the stdout stream by adding a new output redirection case, 2>@1 (i.e. as in Bourne shell syntax), that is only ever valid as the last redirection item in a pipeline. This will enable simple capturing of stderr as well as stdout across platforms.

The behavior is similar to 2>@stdout, but that does not work across platforms. The difference is that directs to a real stdout file channel (which is not available in Windows wish for example), whereas 2>@1 would redirect to the output result for exec/open.

The intention is to handle the common case of wanting to capture stderr as well as stdout output from a single command. This operator is only valid as the last redirection operator. To redirect stderr of previous commands along with their stdout, use the preexisting |& pipe operator.

This is backwards compatible as 2>@1 currently raises an error that "1" is not a valid file id (Tcl does not create such file ids).

Examples

 set fid [open "|$cmd 2>@1"]
 set result [exec $cmd 2>@1]

Reference Implementation

See SF Tcl Patch 957132 http://sf.net/tracker/?func=detail&aid=957132&group_id=10894&atid=310894 for details.

Copyright

This document has been placed in the public domain.