How to append multiple lines to a file
# possibility 1:
echo "line 1" >> greetings.txt
echo "line 2" >> greetings.txt
# possibility 2:
echo "line 1
line 2" >> greetings.txt
# possibility 3:
cat <<EOT >> greetings.txt
line 1
line 2
EOT
If sudo (other user privileges) is needed to write to the file, use this:
# possibility 1:
echo "line 1" | sudo tee -a greetings.txt > /dev/null
# possibility 3:
sudo tee -a greetings.txt > /dev/null <<EOT
line 1
line 2
EOT
printf '%s\n %s\n' 'Host localhost' 'ForwardAgent yes' >> file.txt
Or, if it's a literal tab that you want (rather than the four spaces in your question):
printf '%s\n\t%s\n' 'Host localhost' 'ForwardAgent yes' >> file.txt
You can achieve the same effect with echo
, but exactly how varies from implementation to implementation, whereas printf
is constant.
echo -e "Hello \nWorld \n" >> greetings.txt