How to write to a named file descriptor in bash?

I created a named file descriptor {out} :

  $ exec {out}> out

But when I try to write to the named file descriptor, a new file is created with the file descriptor name:

  $ echo> & {out}
 $ find.  -name {out}
 ./{out}

The Bash manual says:

Each redirect, which may be preceded by a file descriptor number, may be preceded by a word of the form {varname}.

If I do this with a number, it works fine:

  $ exec 3> out
 $ echo test> & 3
 $ cat out
 test

How to do the same with a named file descriptor?

+6
source share
2 answers

The line inside the braces is just the name of the variable that the shell will set for you, the value of which is the shell file descriptor. Brackets are not part of the name. To use it, simply expand the variable in the appropriate context.

 $ exec {out}>out $ echo foobar >&$out $ cat out foobar 

In your example, the file {out} was created

 echo >&{out} 

not the initial exec , the redirection of which created the out file, stored the selected file descriptor in the out variable.

+8
source

echo test >&${out}

If you execute exec {out}>out , then $out gives you the number fd.

+1
source

Source: https://habr.com/ru/post/977336/


All Articles