Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write a shell script that will add an apt repository.
I know that I can do it using sudo add-apt-repository -y <repo>.

My question is can I do it only if the repository was not added already, something like:

if repo was not added yet:
  sudo add-apt-repository -y <repo>
  sudo apt-get update


share|improve this question
add-apt-repository will only add it once; the interesting part is doing the apt-get update conditionally. – Beni Cherniavsky-Paskin Aug 3 '13 at 22:11
up vote 3 down vote accepted

I ended up writing a function to deal with ppa repositories.

add_ppa() {
  grep -h "^deb.*$1" /etc/apt/sources.list.d/* > /dev/null 2>&1
  if [ $? -ne 0 ]
    echo "Adding ppa:$1"
    sudo add-apt-repository -y ppa:$1
    return 0

  echo "ppa:$1 already exists"
  return 1

I wonder if there is some more elegant way.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.