Is there a way to show notification from bash script in Ubuntu?

108,883

Solution 1

Found another way, through Zenity

echo 'message:hi' | zenity --notification --listen

or like this:

zenity --notification --text "System update necessary!" 

(This also has the benefit of already being installed on Ubuntu.)

Solution 2

Tested on Ubuntu 14.04, 16.04, 18.04, 20.04. Screenshots from Ubuntu 20.04.

  1. [WORKS WELL] Popup notification that auto-closes after 4~10 seconds (somehow tied to your OS settings?):

    notify-send "Hello world"
    

    enter image description here
    Source: https://superuser.com/a/31919/425838

  2. Popup window with buttons to click:

    1. Window does NOT get auto-focus: Source: myself; note: for older Unity-based versions of Ubuntu, such as 16.04, -t is ignored for all values except 0--how stupid. :(. For newer Gnome-based versions of Ubuntu, such as 18.04 or 20.04, -t is ignored entirely. Therefore, on older Unity-based versions of Ubuntu, such as 16.04, using -t 0 causes buttons to show up, but on newer Gnome-based versions, it does NOT. That means that for the Ubuntu 20.04 screenshot shown below, the behavior and look of notify-send -t 0 "Hello world" is exactly identical to notify-send "Hello world" above.

      notify-send -t 0 "Hello world"
      

      enter image description here

      On Ubuntu 18.04 or 20.04 or later, just add -u critical to the command instead to get it to stay open indefinitely until you click anywhere on it!:

      notify-send -u critical "Hello world"
      

      enter image description here
      Source: @lucidbrot's comment below this answer, plus my own testing.

    2. OR Window DOES get auto-focus:

      zenity --info --title "Hello" --text "World"
      

      Note: the window will NOT close until you click the OK button.
      enter image description here
      Source: https://askubuntu.com/a/804475/327339

  3. [MY FAVORITE] The window auto-closes after the specified --timeout in seconds, OR after you click the "OK" button!

    zenity --info --title "Hello" --text "World" --timeout=2
    

    Note: the window WILL automatically close after the specified timeout above, in seconds!
    enter image description here
    Source: myself reading the man pages: man zenity

  4. [super ugly-looking]

    xmessage 'hello world'
    

    Note: the window will NOT close until you click the okay button.
    enter image description here
    Source: http://www.linux-commands-examples.com/xmessage

Play sounds too

  1. If you want to play sounds too, along with the window popup, to signify the completion of a command or something, see my other answer here: AskUbuntu.com: How to make a sound once a process is complete?

Solution 3

For KDE users:

$ kdialog --title "Long process completed!" --passivepopup "This popup will disappear in 5 seconds" 5 &

Solution 4

There's also xmessage that will pop-up a window, so it should work on any X11 system.

Pro: It also allows interactively prompting the user with buttons.

Con: Like any pop-up alert, it typically receives focus, so if you're in the middle of typing it can disappear before you read the message.

Solution 5

There exists a cross-platform solution called Yfiton:

$ yfiton -n desktop -Pmessage="Lunch time!" -Pposition=TOP_RIGHT
Share:
108,883

Related videos on Youtube

CTarczon
Author by

CTarczon

Yet another developer. All my code samples you could find here are given freely to the public domain on an "as is" basis, but no warranty is given (implied or assumed).

Updated on September 17, 2022

Comments

  • CTarczon
    CTarczon over 1 year

    Most of the application can show nicely formatted notification on events that appear on top right corner of the screen. I'm about to write a bash script that will do fairy long processing in the background and I really want to know when it is finished. How can I show that nice notification from a bash script?

  • Admin
    Admin over 14 years
    Thanks, just found it myself :) apt-get install libnotify-bin have to be run before to get it.
  • CTarczon
    CTarczon over 14 years
    This is a little bit different as it doesn't use ubuntu desktop notifications.
  • geek
    geek over 14 years
    Yes, this is an alternative that you can use with any Linux distribution and any WM/DE.
  • Malabarba
    Malabarba about 12 years
    Is it possible to set an icon for the notification?
  • Nik Reiman
    Nik Reiman about 11 years
    Con: It looks ugly as hell, and also is a super tiny window which is not always obvious to the user. Anyways, it is universal though. :)
  • Admin
    Admin over 10 years
    How does the root user send a message from crons, init.d, etc?
  • Abhay Mittal
    Abhay Mittal almost 10 years
    xmessage doesn't work in Fedora though. Its not installed by default.
  • Admin
    Admin almost 10 years
    Eg: notify-send Test "Hello World"
  • Admin
    Admin about 8 years
    The --expire-time parameter does not work on Ubuntu thanks to this "feature" (bug). bugs.launchpad.net/ubuntu/+source/notify-osd/+bug/390508
  • Admin
    Admin over 7 years
    I found that notify-send is inhibited during video/audio playback. While this is a valid use case, if you still want to see them then --urgency=critical must be added.
  • Admin
    Admin about 7 years
    I'm using jessie (Linux raspberrypi 4.4.50-v7+), on raspberry, install notify 'libnotify-bin', but it don't bring any notification! -> notify-send Test "Hello World"
  • Admin
    Admin over 6 years
    Thanks buddy. I used this to send me a notification every 10 minutes to blink eyes using cron as a reminder to follow 20-20 (10-10 rule in my case) rule. ;)
  • Waffle's Crazy Peanut
    Waffle's Crazy Peanut over 6 years
    I like Zenity in that it supports user interactions for dialogs (unlike notify-send)
  • friederbluemle
    friederbluemle over 6 years
    It's not available in the default Xorg installation of Arch Linux either.
  • Admin
    Admin about 6 years
    To make it pop up a window with an "OK" and "Cancel" button use notify-send --expire-time=0 "Hello World" or notify-send -t 0 "Hello world"
  • Admin
    Admin about 6 years
    Note, however, that otherwise the --expire-time or -t option is worthless (ignored), unfortunately, and the default of 10 sec or so applies to all notifications regardless of what you want. Pretty stupid if you ask me! askubuntu.com/questions/110969/notify-send-ignores-timeout
  • Gabriel Staples
    Gabriel Staples about 6 years
    You can also get a popup window with an "OK" and "Cancel" button via notify-send --expire-time=0 "Hello World" or notify-send -t 0 "Hello world". Otherwise, however, the -t option is ignored due to some stupid "design decisions": askubuntu.com/questions/110969/notify-send-ignores-timeout
  • Gabriel Staples
    Gabriel Staples about 6 years
    I couldn't take it; I had to write my own answer :) superuser.com/a/1310142/425838
  • Admin
    Admin about 6 years
    I couldn't take it; I had to write my own answer :) superuser.com/a/1310142/425838
  • lucidbrot
    lucidbrot almost 4 years
    My notification with -t 0 disappears by itself unless the level is critical just like the others. I'm on Ubuntu 18.04. But the other options work great, and the [super ugly-looking] made me laugh
  • WesternGun
    WesternGun about 3 years
    Doing it in .profile causes an error: The name org.freedesktop.Notifcations was not provided by any .service file. Otherwise it works well. Ubuntu 18.04
  • Admin
    Admin almost 2 years
    very detail. Thank you