Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Temporary redirection of named file descriptors



Hello all!

TL;DR: How can I redirect a named file descriptor (to e.g. /dev/null), but only for one statement?
Neither of these work, and Section 7 doesn't provide an example.

    somefunc {myfd}>/dev/null
    somefunc $myfd>/dev/null

== More context and background ="">

I have a neat little library of commonly-used functions, "zshlib", that I've got set up as an autoloadable module (technically an auto-loaded function, but it exposes 50+ functions to the global namespace and I'm more familiar with Python nomenclature).

Scripts which use "zshlib" load it at the very top of the script.  The first thing thing "zshlib" does is create a named file descriptor:

    exec {zshlog}>&2

This is done for lots of reasons, mainly the "run a command but print it out and properly escaped so the user knows what is taking so long" functions.  The executed command may write to stdout or stderr, and I wanted to ensure my library does not pollute those streams, or prevent them from being grepped / redirected / filtered / etc (as much as possible, anyway).

This is generally achieved per-statement or for a statement-list.

    >$zshlog command echo foo bar

This is all great, but occasionally I want to silence the output from "zshlib".  I assumed either of these syntaxes would work:

    somefunc {zshlog}>/dev/null
    somefunc $zshlog>/dev/null

The first fails with "can't clobber parameter zshlog containing file descriptor 11".
The second simply uses $zshlog as a parameter to somefunc.



Messages sorted by: Reverse Date, Date, Thread, Author