Bluetooth Volume Limiter for Linux — Zorin OS, Ubuntu & Other Compatible Distros

Yo everyone.

I realized I was on the way to get deaf, so I created a permanent script (with chatGPT's help(well it made the whole script actually but it works that's what matters)) to limit maximum volume when using any bluetooth device.

Once configured, if you or the system try to raise the Bluetooth audio volume above your chosen limit, the script automatically brings it back down.

Features

  • Set your preferred maximum volume percentage in one line of the script.
  • Automatically corrects Bluetooth output volume when it exceeds your chosen limit.
  • Runs in the background.
  • Can start automatically using a systemd user service.

Requirements

  • Bash
  • pactl
  • A PulseAudio-compatible audio server
  • systemd user services for automatic startup

I've tested it on Zorin OS 18 Core with my QCY H3 Bluetooth headphones. I haven't tested it on other distributions. Probably works on other distributions too tho.

Anyway to change the limit, edit MAX_VOLUME=50 at the top to your preferred percentage.

I hope this is useful to other users. I was really glad when it worked.

Script

#!/usr/bin/env bash

MAX_VOLUME=50

check_bluetooth_sinks() {
    while IFS= read -r sink; do
        [[ -n "$sink" ]] || continue

        volume=$(
            pactl get-sink-volume "$sink" 2>/dev/null |
                grep -oE '[0-9]+%' |
                tr -d '%' |
                sort -nr |
                head -n 1
        )

        if [[ "$volume" =~ ^[0-9]+$ ]] &&
           (( volume > MAX_VOLUME )); then
            pactl set-sink-volume "$sink" "${MAX_VOLUME}%"
        fi
    done < <(
        pactl list short sinks |
            awk '$2 ~ /^bluez_output\./ {print $2}'
    )
}

check_bluetooth_sinks

pactl subscribe | while IFS= read -r event; do
    case "$event" in
        *" on sink #"*|*" on server #"*)
            check_bluetooth_sinks
            ;;
    esac
done