bashfile-descriptoroutput-redirect

Inserting the output of file descriptor 3 inline in bash


I've written a program in ruby called citeselect that uses curses to dynamically select a reference from a bibtex bibliogrpahy. I would like to put this program into a pipeline to cite anything easily using the output of this program. Unfortunately, as I found out from Ncurses and linux pipeline (c), Curses uses stdout for its display.

Therefore, I've routed the output citation key into file descriptor 3 when it is provided as an output. I've verified that it works:
citeselect 3>output

Is there any way to capture the output sent to fd3 in a one liner in bash? Something like
echo "The citation key is $(citeselect 3>)"

Thanks.


Solution

  • Using Victory's answer as a starting point, and after experimenting around with output redirection, I realised that I had the wrong idea about what n>&m did. This guide really helped me:
    http://mywiki.wooledge.org/BashFAQ/002

    To do this I have to redirect stdout to stderr, and then fd3 to stdout like this:
    CITATION=$(citeselect 3>&1 1>&2)

    That way curses is still able to use the tty via the stderr stream, while I can still pipe the citation output. In a lot of my earlier attempts, I had the redirection arguments reversed because of a fundamental misunderstanding of what they were doing.