adding a line to file

i am writing a script which will let user to input a line.
i m not sure how do i add this line to the end of a txt file ?

echo $line >> file.txt
1 Like
read add
sed '$ a $add' file.txt

it displayed the file and i can see the line was added
But i donno why when i opened the file to check the line was not there

With sed you can do it like this:

read add
sed -i '$ a $add' file.txt

i tried

and it works but how do i make it a new line?

What do you mean? Empty line before added one?

the input was added to the last line of the file
how do i make it start on a new line?

Thanks!! :smiley:

With this?

echo $line >> file.txt

Impossible.

two ways :

echo "
$line" >> file.txt

or

echo -e "\n$line" >> file.txt

In any case, quotes prevent trouble from spaces...
P.S: You'll have to know that echo will put a newline after, else use the -n option of echo.