Georgi Georgiev
Georgi Georgiev

Reputation: 1623

Redirecting file descriptors

guys!

What exactly does this do?

exec 4<&1

I understand this as "make the output an input for fd 4". But if so, why the following doesn't work?

echo a 4>temp.log

I expected that this will print "a" on the screen and store it in temp.log file as well. I know I can do the same with tee, but I don't know why it doesn't work like this.

Where am I wrong?

Upvotes: 2

Views: 76

Answers (1)

Juan Cespedes
Juan Cespedes

Reputation: 1363

<& is to duplicate input file descriptors. 4<&1 means to create a new file descriptor, 4, which will get the same contents as fd 1 when reading from it.

What you need to write your stdout to fd 4 is 1>&4. However, it will not work, because the file descriptor 4 does not yet exist. You would have to do:

exec 4>temp.log
exec 1>&4

The first line redirects all the output to fd 4 to that file; the second redirects the standard output to fd 4. However, it will still not do what you want; "duplicating" file descriptors does not mean that the output is sent to 2 places, but that it will be sent to anothe place. After those sentences, all the output will be sent to "temp.log", but not to the previous standard output anymore.

Upvotes: 3

Related Questions