title: Is echo atomic when writing single lines
body: <p>I am currently trying to get a script to write output from other started commands correctly into a log file. The script will write it's own messages to the log file using echo and there is a method to which I can pipe the lines from the other program.</p>
  
  <p>The main problem is, that the program which produces the output is started in the background, so my function that does the read may write concurently to the logfile. Could this be a problem? Echo always only writes a single line, so it should not be to hard to ensure atomicity. However I have looked in google and I have found no way to make sure it actually is atomic.</p>
  
  <p>Here is the current script:</p>
  
  <pre><code>LOG_FILE=/path/to/logfile
  
  write_log() {
    echo "$(date +%Y%m%d%H%M%S);$1" &gt;&gt; ${LOG_FILE}
  }
  
  write_output() {
    while read data; do
      write_log "Message from SUB process: [ $data ]"
    done
  }
  
  write_log "Script started"
  # do some stuff
  call_complicated_program 2&gt;&amp;1 | write_output &amp;
  SUB_PID=$!
  #do some more stuff
  write_log "Script exiting"
  wait $SUB_PID
  </code></pre>
  
  <p>As you can see, the script might write both on it's own as well as because of redirected output. Could this cause havok in the file?</p>