If you need to read in the output of a command, you could use ed as in the linked question, with this variation:
ed -s test.txt <<< $'0r !echo stuff\nw\nq' This reads the output of the command echo stuff into test.txt after line zero.
To insert multi-line text before the 1st line via here-doc you'd run
ed -s test.txt <<EOT 1i add some line and some more to the beginning . w q EOT The dot signals the end of input-mode which means the last solution assumes your text doesn't contain lines consisting of single dots.