How can I append text to /etc/apt/sources.list from the command line?


Question

I am new to linux, and just beginning to learn bash. I am using Ubuntu 9.04, and would like to add repositories to /etc/apt/sources.list from the command line. Basically, I would like to do this:

sudo echo "[some repository]" >> /etc/apt/sources.list

However, even when I use sudo, I get this error:

bash: /etc/apt/sources.list: Permission denied

How do I avoid this error?

1
73
5/12/2009 1:13:48 AM

Accepted Answer

In Karmic, you can just use the add-apt-repository command, at least for PPAs.

For example:

sudo add-apt-repository ppa:docky
1
11/12/2009 9:56:00 PM

echo "[some repository]" | sudo tee -a /etc/apt/sources.list

The tee command is called as the superuser via sudo and the -a argument tells tee to append to the file instead of overwriting it.

Your original command failed, as the IO redirection with >> will be done as the regular user, only your echo was executed with sudo.

Calling a sudo subshell like

sudo sh -c 'echo "[some repository]" >> /etc/apt/sources.list'

works, too as pointed out by others.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon