Exec command behaviour

Hello All,

I came across this line in a shell script that I was looking at yesterday. Couldn't get a clue of what it does. :confused:

....
....
blah blah
....
.....

exec >&- 2>&-

......
blah blah
.....

Can someone explain me this??

Usually I have seen exec commands used to redirect std output and error output to log files. And this one is different! :frowning:

Looks like that program is closing stdout and stderr. C.f. man bash :

So RudiC,

I have this

exec >file1 2>&1
cmd1
cmd2
cmd3
exec >&- 2> &-
cmd4
cmd5

By your explanation, file1 will have the outputs of cmd1, cmd2 & cmd3.
Then exec >&- 2>&- will stop redirecting to file1. Thus outputs of cmd4 & cmd5 will be directed to stdout & stderr.

Is my understanding correct?

---------- Post updated at 03:12 PM ---------- Previous update was at 03:12 PM ----------

So RudiC,

I have this

exec >file1 2>&1
cmd1
cmd2
cmd3
exec >&- @> &-
cmd4
cmd5

By your explanation, file1 will have the outputs of cmd1, cmd2 & cmd3.
Then exec >&- 2>&- will stop redirecting to file1. Thus outputs of cmd4 & cmd5 will be directed to stdout & stderr.

Is my understanding correct?

No, it closes stdout and stderr entirely - your shell won't have any output channel any more.

1 Like