The shell processes ">", "<", ">>" etc itself before launching commands. So the problem is that "sudo >> /etc/foo" tries to open /etc/foo for append before gaining privileges.
One way round this is to use sudo to launch another shell to do what you want, e.g.:
sudo sh -c 'echo "[some repository]" >> /etc/apt/sources.list'
Or alternatively:
echo "[some repository]" | sudo sh -c 'cat >> /etc/apt/sources.list'
A simpler approach may simply be to use sudo to launch an editor on the /etc/file :)
It's better to use a separate file in /etc/apt/sources.list.d rather than modifying /etc/apt/sources.list, as explained in this other answer. (Note that the file name MUST end in .list or it will be ignored.)
However, if you want to create it using echo the issue with permissions remains. You can use tee to create it like this:
echo '[some repository]' | sudo tee /etc/apt/sources.list.d/some-repository.list >/dev/null
or like this:
sudo tee /etc/apt/sources.list.d/some-repository.list >/dev/null <<EOF
[some repository]
EOF
Note that you don't need -a on the tee command (because you're not appending).
You can also create the file somewhere else and then copy it into place with:
If you were to switch to the root user the same command will work just fine. This is because sudo elevates the priveleges only for the [echo] command and does not elevate the right side of the output redirection.
sudo su
echo "[some repository]" >> /etc/apt/sources.list
If you do it this way be sure to exit from su so that you are not running unnecessary programs as root (superuser)
interesting, 1- remove the file with rm , 2 create the file again with touch, 3 use printf to print formatted, 4 pipe with tee to the file(THIS IS FOR DEBIAN) replace to your tastes and likes
sudo rm /etc/apt/sources.list && sudo touch /etc/apt/sources.list && sudo chmod +rwx /etc/apt/sources.list && sudo printf "deb http://deb.debian.org/debian buster main contrib non-free
deb-src http://deb.debian.org/debian buster main contrib non-free
deb http://deb.debian.org/debian-security/ buster/updates main contrib non-free
deb-src http://deb.debian.org/debian-security/ buster/updates main contrib non-free
deb http://deb.debian.org/debian buster-updates main contrib non-free
deb-src http://deb.debian.org/debian buster-updates main contrib non-free" | sudo tee -a /etc/apt/sources.list