Q:

How to insert text before the first line of a file?

I've been looking around sed command to add text into a file in a specific line.
This works adding text after line 1:
sed '1 a\

But I want to add it before line 1. It would be:
sed '0 a\

but I get this error: invalid usage of line address 0.
Any suggestion?

A:

Use sed's insert (i) option which will insert the text in the preceding line.
sed '1 i\

Question author's update:
To make it edit the file in place - with GNU sed - I had to add the
   -i option:
sed -i '1 i\anything' file

Also syntax
sed  -i '1i text' filename

For non-GNU sed 
You need to hit the return key immediately after the backslash 1i\ and after first_line_text:
sed -i '1i\
first_line_text
'

Also note that some non-GNU sed implementations (for example the one on macOS) require an argument for the -i flag (use -i '' to get the same effect as with GNU sed).

A:

Echo is used to get the text. Cat filename - prints the file in the console and > uses it to send to another file filename1 and then we move filename1 to filename to get the text inserted to the first line of desired file. 
  (echo "some text" && cat filename) > filename1 && mv filename1 filename

A:

You can use the POSIX tool ex:
ex a.txt <<eof
1 insert
Sunday
.
xit
eof

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html

