how to check if PPA is already added to apt sources list in a bash script

Do someone know how to check if a PPA is already added to my system before i add it with the add-apt-repository command in a shell script.

That would be very helpful.

Thank you.


Solution 1:

You can do something like this:

the_ppa=...  # e.g. the_ppa="ondrej/apache2"

if ! grep -q "^deb .*$the_ppa" /etc/apt/sources.list /etc/apt/sources.list.d/*; then
    # commands to add the ppa ...
fi

Notes:

  • grep exits with success if it finds a match
  • The ! negates this, so the commands will be executed if there is no match = ppa not added
  • The -q flag makes grep quiet, so it doesn't print the matched lines. It would be just noise, we need only to know if there is a match or not, for which the exit code is perfect
  • The ^deb .* prefix is to make sure the matched line starts with deb, to exclude lines that are commented out

Solution 2:

Open your terminal and type this command

grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/* | grep fogger

replace fogger with PPA name.

enter image description here