Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Showing a GUI Notification From a Shell Script in Linux
GUI notifications from shell scripts in Linux allow you to display visual alerts and messages to users. Two primary tools accomplish this: notify-send for desktop notifications and zenity for interactive dialog boxes.
Using the notify-send Command
The notify-send command is part of the libnotify library and comes pre-installed on most Linux distributions. It displays brief desktop notifications that appear in the system notification area.
Basic Syntax
notify-send [options] <title> <message>
Simple example displaying a notification
notify-send "Hello" "World"
Common Options
| Option | Description | Example Values |
|---|---|---|
-u, --urgency=LEVEL |
Sets urgency level | low, normal, critical |
-t, --expire-time=TIME |
Duration in milliseconds | 5000 (5 seconds) |
-i, --icon=ICON |
Notification icon | /path/to/icon.png |
-c, --category=TYPE |
Notification category | email, im, device |
Advanced notification example
notify-send -u critical -t 5000 -i dialog-warning "System Alert" "Disk space is running low"
Notifications in Shell Scripts
Basic shell script with notification
#!/bin/bash # Script commands here echo "Processing files..." sleep 3 # Send completion notification notify-send "Script Complete" "File processing finished successfully"
Using variables for dynamic notifications
#!/bin/bash # Define notification content title="Backup Status" message="Backup completed at $(date)" # Send notification notify-send "$title" "$message"
Conditional Notifications
#!/bin/bash
# Simulate a task
if some_command; then
notify-send -u normal "Success" "Task completed successfully"
else
notify-send -u critical "Error" "Task failed to complete"
fi
Using the zenity Utility
Zenity displays GTK+ dialog boxes and is ideal for interactive notifications that require user input. It provides more visual options than notify-send.
Dialog Types
Information dialog
zenity --info --title="Information" --text="Process completed successfully"
Warning dialog
zenity --warning --title="Warning" --text="System resources are low"
Error dialog
zenity --error --title="Error" --text="Failed to connect to server"
Question dialog with user input
if zenity --question --title="Confirm" --text="Do you want to continue?"; then
echo "User selected Yes"
else
echo "User selected No"
fi
Advanced zenity Examples
Text entry dialog
name=$(zenity --entry --title="User Input" --text="Enter your name:") echo "Hello, $name"
Progress bar notification
(for i in {1..100}; do
echo $i
sleep 0.1
done) | zenity --progress --title="Processing" --text="Please wait..." --auto-close
File selection dialog
file=$(zenity --file-selection --title="Select File" --file-filter="Text files | *.txt") echo "Selected file: $file"
Comparison
| Feature | notify-send | zenity |
|---|---|---|
| Purpose | Desktop notifications | Interactive dialogs |
| User Interaction | None (display only) | Buttons, input fields |
| Appearance | System notification area | Modal dialog windows |
| Dependencies | libnotify | GTK+ libraries |
| Best For | Status updates, alerts | User input, confirmations |
Conclusion
Both notify-send and zenity provide effective ways to display GUI notifications from shell scripts. Use notify-send for simple status notifications and zenity when you need user interaction or more complex dialog functionality. Choose the appropriate tool based on whether you need passive notifications or active user engagement.
