what does "exec 5>>foobar.txt" do?

-1

What does the shell command

exec 5>>foobar.txt

do? I can't google it for obvious reasons, and otherwise not sure where to start looking.

user322908

Posted 2016-03-11T11:53:24.277

Reputation: 739

Why was this question down voted? I found it quite useful. – MJ Walsh – 2019-04-04T17:01:43.993

Answers

0

As others have said, it opens a file descriptor to append to the named file.

You would use this file descriptor like this:

echo "hello world" >&5
date >&5
while read line; do some_transformation; done < input_file >&5

glenn jackman

Posted 2016-03-11T11:53:24.277

Reputation: 18 546

3

The command in question redirects file descriptor 5 to a file foobar.txt.

As for "where to start looking" - this answer is a very good starting point.

techraf

Posted 2016-03-11T11:53:24.277

Reputation: 4 428

2To be more precise, a new shell is created to replace the current shell, but with any writes to file descriptor 5 appended to a file named foobar.txt in the current directory. Any program waiting for completion of the shell will wait for the new shell and receive its eventual exit status. – AFH – 2016-03-11T12:56:05.647