Appending text to file
There are multiple ways to accomplish this, depending on what needs to be appended.
Append single line to a file
Using echo and pipe
$ echo "127.0.0.1 localhost" >> /etc/hosts
Using echo and pipe with sudo
sudo bash -c 'echo "127.0.0.1 localhost" >> /etc/hosts'
Using tee
$ echo echo "127.0.0.1 localhost" | tee -a /etc/hosts
Using tee with sudo
echo "echo "127.0.0.1 localhost" | sudo tee -a /etc/hosts
Append multiple lines to a file
Using echo and pipe
$ echo "127.0.0.1 localhost
> 127.0.0.2 localhost
> 127.0.0.3 localhost" >> /etc/hosts
Using cat
$ cat << EOF >> /etc/hosts
> 127.0.0.1 localhost
> 127.0.0.2 localhost
> EOF
Using tee
$ cat << EOF | tee -a /etc/hosts
> 127.0.0.1 localhost
> 127.0.0.2 localhost
> EOF
Stdout and stderr
Stderr stream is not returned by default.
Append stdout and stderr to a file
$ /bin/bash script.py &>> log.txt
Append stdout to one file and combination of both to another
$ /bin/bash script.py >> output.txt 2>> stdoutwithstderr.txt