What is the point of creating a file descriptor and then echoing to it

I see in some Linux bash configurescripts, such as scripts generated autoconf, sequences like this

exec 5>>config.log

(...)

echo foo >&5

(...)

echo bar >&5

(repeated many times)

I think you can just do

echo foo >>config.log

which is easier. So what is the point of doing this with exec. There must be some reason that I do not understand. What is it?

+4
source share
1 answer

Convenience. If the target log file name changes, you will have to search and replace all instances. This is annoying. You can slightly reduce this variable:

$LOGFILE=config.log
echo foo>>$LOGFILE

. bash LOGFILE - . bash fopen - . bash fwrite . bash finally fclose it. , , , bash . , , . , ? .

. , , :

  • (, stderr)
+5

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


All Articles