Usando uma função e o GNU Parallel você substitui a seção repetitiva:
cd ~
apt-get update
apt-get install varnish -y
sed -i 's/Listen 80/Listen 8080/g' /etc/apache2/ports.conf
myfunc() {
sed -i 's/\*\:80/\*\:8080/g' /etc/apache2/sites-available/$1 &&
a2ensite $1
}
export -f myfunc
parallel myfunc {/} ::: /etc/apache2/sites-available/*
mkdir -p /etc/systemd/system/varnish.service.d # Be aware! You might not need this in the future.
cat <<-'VARNISH' > /etc/systemd/system/varnish.service.d/customexec.conf
[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
VARNISH
systemctl restart apache2.service && systemctl daemon-reload && systemctl restart varnish.service