The following command works as expected:
# Add/create /tmp/hosts with the string content inside EOFs
$ cat >> /tmp/hosts <<EOF
127.0.0.0 example.com
127.0.0.0 foo.example.com
EOF
However, the following command yields an error even when the sudo'd account has a permission to write to /etc/hosts
.
$ sudo cat >> /etc/hosts <<EOF
127.0.0.0 example.com
127.0.0.0 foo.example.com
EOF
-bash: /etc/hosts: Permission denied
Why? And how can one achieve exactly what the above command intends to?