prepend **heredoc** to a file, in bash

bash, herestring, prepend

Solution

You can use `awk` to insert a multiline string at beginning of a file:

awk '1' <(echo "$MSG") file

Or even this `echo` should work:

echo "${MSG}$(<file)" > file

Problem

I was using: ``` cat <<<"${MSG}" > outfile ``` to begin with writing a message to `outfile`, then further processing goes on, which appends to that `outfile` from my awk script. But now logic has changed in my program, so I'll have to first populate `outfile` by appending lines from my `awk` program (externally called from my bash script), and then as a final step prepend that ${MSG} `heredoc` to the head of my `outfile`.. How could I do that from within my bash script, not awk script? EDIT this is MSG `heredoc`: ``` read -r -d '' MSG << EOF ----------------------------------------------- -- results of processing - $CLIST -- used THRESHOLD ($THRESHOLD) ----------------------------------------------- l EOF # trick to pertain newline at the end of a message # see here: http://unix.stackexchange.com/a/20042 MSG=${MSG%l} ```

Original source