I am surprised that in this old question nobody has shown the most common (and quite simple in this case) sed command:
$ sed -i '1s/^/new_text\
/' file_name
Which works in most shells and is portable to several sed versions, provided that the file contains at least one valid line.
If GNU sed is available, you may use this:
$ sed -i '1s/^/new_text\n' file_name
The difference is that GNU sed allow the use of a \n
for a newline and others need a literal newline preceded by a backslash (which also work in GNU sed anyway).
If a shell which accepts the $'…'
syntax is in use, you may insert the newline directly, so sed sees that the newline is already there:
$ sed -i $'1s/^/new_text\\\n/' file_name
Which works for more sed versions.
If the file has no lines (i.e: its empty) just:
$ echo "new_text" > file_name