Add end of char \n on end of file

Hi,

I want to add \n as a EOF at the end of file if it does't exist in a single command. How to do this?

when I use command

echo "1\n" > a.txt 

and

od -c a.txt
0000000   1  \n  \n
0000003

How does it differentiate \n and eof in this case?

Regards,
Venkat

If you look at the output of od -c, you'll notice that it has a backslash followed by an n, with a space between them. Then a \n with no space.

If you want to literally append a newline character to a file, use the -e flag to echo.

 $ echo "1\n" > a.txt
 $ echo -e "1\n" > b.txt
 $ wc -c a.txt
4 a.txt
 $ wc -c b.txt
3 b.txt