Showing a GUI Notification From a Shell Script in Linux


There are several ways to display a GUI notification from a shell script in Linux, but one common method is to use the notify-send command. This command is part of the libnotify library and is typically pre-installed on most Linux distributions.

An example of how to use notify-send to display a notification with a title of "Hello" and a message of "World" is −

notify-send "Hello" "World"

You can use the command in your shell script by simply adding the command to it, or assign the notification text to a variable and use it

message="This is your reminder"
notify-send "Reminder" "${message}"

Additionally, you can customize the appearance and behavior of the notification using various options, such as specifying an icon, setting the urgency level, or controlling the duration of the notification.

Using the notify-send Command

The notify-send command is used to display a GUI notification from the command line or from a script. The basic syntax for the command is as follows −

notify-send [options] <title> <message>

Where <title> is the title of the notification and <message> is the body text of the notification.

Here are some common options you can use with the notify-send command −

-u, --urgency=LEVEL - Specifies the urgency level of the notification. LEVEL can be one of "low", "normal", "critical".

-t, --expire-time=TIME - Specifies the duration of the notification in milliseconds.

-i, --icon=ICON - Specifies the icon to be used with the notification.

-c, --category=TYPE - Specifies the notification type (e.g., "email", "im").

-h, --hint=TYPE:NAME:VALUE - Specifies a hint to be passed to the notification server.

You can use an example like this −

notify-send -u critical -t 5000 -i /path/to/icon.png "Title" "The message"

This command will show a critical notification that expires after 5 seconds and uses the icon located at /path/to/icon.png .

Sending Notifications From Our Shell Script

You can send notifications from your shell script by using the notify-send command, just like you would from the command line. Here's an example of a simple shell script that sends a notification with a title of "Script Complete" and a message of "The script has finished running" −

#!/bin/bash

# your script commands go here

# send the notification
notify-send "Script Complete" "The script has finished running"

You can also use variables to store the notification title and message, and then pass them to the notify-send command.

#!/bin/bash

# your script commands go here

# define the title and message
title="Script Complete"
message="The script has finished running"

# send the notification
notify-send "$title" "$message"

You can also send a notification when a specific task is done or failed, for example when a task has a specific status −

#!/bin/bash

# your script commands go here

# Define the title and message
title="Task Failed"
message="The task has failed to run"

if task_status -eq 0; then
  title="Task Complete"
  message="The task has been completed successfully"
  notify-send -u normal "$title" "$message"
else
  notify-send -u critical "$title" "$message"
fi

This script will check the status of the task. If the task completed successfully, it will send a normal notification with the title "Task Complete" and the message "The task has been completed successfully", otherwise it will send a critical notification with the title "Task Failed" and the message "The task has failed to run".

Using the zenity Utility

Another way to display a GUI notification from a shell script in Linux is to use the zenity utility. zenity is a command-line tool that allows you to display GTK+ dialogs in command-line and shell scripts. It can be used to display a variety of dialogs, including alerts, error messages, and question prompts.

Here is an example of how to use zenity to display a notification with a title of "Hello" and a message of "World" −

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

You can also use the --warning, --error, --question options instead of --info to display different types of notifications. The --title option sets the title of the dialog, and the --text option sets the text of the dialog.

You can also use zenity to display a notification with a progress bar, for example −

(for i in {1..100}; do echo $i; sleep 0.1; done) | zenity --progress --title="Title" --text="Your process is running" --auto-close --auto-kill

This command will show a progress bar that closes and kills itself after reaching 100%.

You can use the zenity command in your shell script in the same way you would use it from the command line, by simply adding it to your script and passing any necessary options and arguments.

Displaying Dialog and Notification From Our Shell Script

zenity is a command line tool that can be used to display various types of dialog boxes and notifications in a graphical user interface (GUI) from shell scripts.

Here are some examples of the different types of dialogs and notifications that you can display using zenity −

Information dialog −

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

Warning dialog −

zenity --warning --title="Warning" --text="This is a warning message"

Error dialog −

zenity --error --title="Error" --text="An error has occurred"

Question dialog −

zenity --question --title="Question" --text="Do you want to continue?"

Text entry dialog −

zenity --entry --title="Enter your name" --text="Please enter your name"

File selection dialog −

zenity --file-selection --title="Select a file" --file-filter='*.txt'

List dialog

zenity --list --title="Select an option" --column "Option" --column "Description"  --print-column=2 "Option 1" "This is the first option" "Option 2" "This is the second option" "Option 3" "This is the third option"

You can use the zenity command in your shell script by simply adding it to your script, and passing any necessary options and arguments.

You can also use the output of the zenity command in your script by storing it in a variable, for example −

response=$(zenity --question --title="Question" --text="Do you want to continue?")
if [ "$response" = "0" ]; then
  # user clicked OK
else
  # user clicked Cancel
fi

Keep in mind that zenity requires the GTK+ libraries to be installed in the system, it may not be present in some minimalistic distros, if that's the case, you may need to install it manually.

Conclusion

There are several ways to display GUI notifications and dialogs from a shell script in Linux, including using the notify-send command and the zenity utility. notify-send is a command-line tool that can be used to send desktop notifications, and it's typically pre-installed on most Linux distributions. zenity, on the other hand, is a command-line tool that can be used to display GTK+ dialogs in command-line and shell scripts, it provides more options than notify-send and can be useful to display various types of dialogs and notifications.

Updated on: 24-Jan-2023

776 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements