[clug] capturing stdout of shell function without subprocess or tempfile

Kevin Pulo kev at pulo.com.au
Wed Aug 19 23:28:02 MDT 2009

All this scripting fun reminded me of a problem I had recently and
couldn't solve.

It's easy (in bash) to feed the contents of a variable to a program
without using an extra process by using a here-string:

    somecommand <<< "$foobar"

In the bad old days that had to be a full here-document:

    somecommand <<-EOF

or worse, something stupid like:

    echo "$foobar" | somecommand

or else you had to use a temporary file:

    echo "$foobar" > tmpfile
    somecommand < tmpfile

Tempfiles are a pain, because then you have to set up traps to get rid
of them (which still don't help for SIGKILL), where to put them,
unique names, races, they assume a read-write filesystem, they're
slower than being in-memory, etc etc.

I want the reverse.  I want to grab the output of a shell function,
and stick it in a variable.  But, the catch is, I don't want to use
tempfiles, and I don't want the shell function running in a subprocess
(because the function has side-effects, ie. it sets important

So things like:

    somefunction | read foobar
    read foobar < <(somefunction)

and so on are no good.

Is it just me, or is this impossible?  Does bash need something like
an inverse here-string added to it to be able to do this, eg:

    somefunction >>> foobar


To put it another way, if I have something like this:

    function func {
        foo=$(($foo + 1))
        echo 2
    func > "$tmpfile"
    read bar < "$tmpfile"
    rm -f "$tmpfile"
    # each no good:
    #read bar < <(func)
    #func | read bar
    bar=$(($bar + 1))
    echo "$foo $bar"

I want to get rid of the tmpfile use and still have it output
correctly (ie. "2 3").  Wishful thinking?


| Kevin Pulo                Quidquid latine dictum sit, altum viditur. |
| kev at pulo.com.au               _ll l_ng__g_e_ _r_ hi__ly p__d_ct__le. |
| http://www.kev.pulo.com.au/         God casts the die, not the dice. |
`--------------- Linux: The choice of a GNU generation. ---------------'
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/linux/attachments/20090820/8fb322db/attachment.pgp>

More information about the linux mailing list