Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does rewriting a file with "envsubst <file >file" leave it empty? [duplicate]

I need to put some environment variables values to file.

cat file
# $VAR

echo $VAR
# text

When I do envsubst '$VAR $VAR' < file > file file becomes empty. To resolve this issue I use envsubst '$VAR $VAR' < file | tee file 2>&1 >/dev/null. But sometimes it doesn't work, I mean file becomes empty. The first question is why sometimes file becomes empty and the second is what is the TRUE Unix way to put env vars to file in Linux and *BSD and MacOS ?

like image 577
Konstantin Shestakov Avatar asked Nov 02 '17 17:11

Konstantin Shestakov


1 Answers

The shell always processes the output redirection, which truncates the file, before starting the command.

With the pipeline envsubst ... < file | tee file, the two commands envsubst and tee run asynchronously, which means the OS is free to start one or the other command first. You are basically tossing the dice that envsubst runs and reads file before tee runs and truncates the file.

The only correct solution is to write to a temporary file, then replace the original with the temporary file after you have confirmed that the command was successful.

like image 53
chepner Avatar answered Sep 28 '22 03:09

chepner