home | list info | list archive | date index | thread index

[OCLUG-Tech] can anyone explain bash "{fd}" file descriptor notation?

  • Subject: [OCLUG-Tech] can anyone explain bash "{fd}" file descriptor notation?
  • From: "Robert P. J. Day" <rpjday [ at ] crashcourse [ dot ] ca>
  • Date: Mon, 5 May 2014 05:40:03 -0400 (EDT)
  perusing o'reilly's "bash pocket reference" for all of the little
bash tips and tricks i'd never heard of, and ran across this WRT file
descriptors; apparently, bash supports assignment of a new file
descriptor to a named variable, while giving that new fd the next
available open fd number from 10 and up.

  example:

$ echo foo {foofd}> /tmp/foo.out
foo
$

what the above apparently does is that it still echoes "foo" to
stdout, but as a side effect, it opens a new file descriptor (in this
case, 10), i guess associated with the output file /tmp/foo.out.

  there's nothing *in* /tmp/foo.out, but i can see the new fd value:

$ echo ${foofd}
10
$

at this point, it seems i can write into the output file /tmp/foo.out
with:

$ echo test1 >&10
$ cat /tmp/foo.out
test1
$

but it would make more sense to be able to use the named fd, would it
not? however, it doesn't appear i can do this:

$ echo test2 >&{foofd}
$ cat /tmp/foo.out
test1
$

  so there was no error, but that seemed to have no effect. however,
if i treat it as a variable (which i suspect i'm supposed to):

$ echo test2 >&${foofd}
$ cat /tmp/foo.out
test1
test2
$

it *appends* to the file, which is not what i was expecting.

  can anyone clarify the proper use of this construct, and
particularly if they use it on a regular basis for something useful?

rday

-- 

========================================================================
Robert P. J. Day                                 Ottawa, Ontario, CANADA
                        http://crashcourse.ca

Twitter:                                       http://twitter.com/rpjday
LinkedIn:                               http://ca.linkedin.com/in/rpjday
========================================================================