How to clear balloon popup from bash

I am setting a notification with notify-send in a script. The only problem is that when the script is called several times, all the notifications are added to a notification stack and only called one after the other.

Is there a way to clear all notifications on the screen and display the new one ?


Unfortunately, you can not clear or "dismiss" the notify-osd notifications. You might have better luck using Zenity instead; it has more options than notify-send.

You could use the --timeout option to dismiss a notification after some number of seconds has passed.

zenity --info --timeout=5 --title="Test Notification" --text "$(date +%Y%m%d-%H%M%S): My notification"

You could also keep a list of process IDs (in an environment variable or file) of previous notifications and send them a HUP signal to clear them out before displaying a new notification.

i=0
pids=
for x in $(seq 1 5); do
    i=$((i + 1))
    zenity --info --title="Test Multiple Notifications" --text "$(date +%Y%m%d-%H%M%S): Notification number $i" &
    pids+="$! "
done
sleep 5
for p in $pids; do kill -HUP $p >/dev/null 2>&1; done
i=$((i + 1))
zenity --info --timeout=2 --title="Test Multiple Notifications" --text "$(date +%Y%m%d-%H%M%S): Notification number $i" &

Or kill all zenity processes before displaying a new notification:

killall zenity
zenity --info --title="Test Notifications" --text "$(date +%Y%m%d-%H%M%S): My notification" &

Or kill certain zenity processes before displaying a new notification:

ps ho pid,args | grep -i 'zenity.\+--title=test notifications' | sed -e 's/^ *\([0-9]\+\).*$/\1/'
zenity --info --title="Test Notifications" --text "$(date +%Y%m%d-%H%M%S): My notification" &

This is actually feasible using notify-send:

notify-send --hint int:transient:1 "Title" "Body"

By setting the transient hint, the when the notification expires or dismissed, it does not linger around in the notification bar.


Maybe not the best option but here's an alternative: you can simply kill the notify-osd process and restart it. Then publish your notification.

pkill notify-osd
/usr/lib/x86_64-linux-gnu/notify-osd &
notify-send "Hello!"

Unfortunately this does not seem feasible with notifications set by notify-send. Have a look at the source code of notify-send.c - it creates a notification, sets its parameters but does not store any reference to it, anywhere. Instead, it calls g_object_unref, which, as I understand from here, effectively removes the possibility of external interaction with the message. So, to have advanced management power over the notifications, you'd need to use different tool than notify-send (probably a custom application).


A possible crude hack to achieve your goal would be a script using xautomation tools. You could use them to locate the "close" buttons on all the notification pop-ups and simulate a mouse click on each of them. But that's not so easy and certainly not a neat solution.