As already mentioned the straightforward method to append a new line to a file is by means of echo
double redirection. To accomplish the same in sed, you need the a
command and match the end of file with $
sed '$a new line' file
However, since you have the user's input stored in a shell variable, you cannot use single quotes (that prevent shell expansion
). Using double quote you have to protect the $ pattern from the shell, anyway. Here is an example:
sed "\$a $passwd" /var/yp/source/passwd
By default sed sends the result to the standard output. In order to edit the file in place, that is to actually change its content, you need the -i option.
sed -i "\$a $passwd" /var/yp/source/passwd
You can also save a backup copy of the original file, using a custom suffix as argument of the -i option, e.g.
sed -i.bck "\$a $passwd" /var/yp/source/passwd
Just a final note on your script. The line
is incorrect. It should be
without the $ sign. This means you have to specify the variable name in the read statement, not its value! Hope this helps.