How to display GUI dialog in bash script using Zenity
We all know that Linux bash scripts are the real advantage of Linux. Usually, we want to display a graphical user interface (GUI) in the script to simplify the interaction with the user. The GUI makes any script more user-friendly and beautiful.
For GTK in Shell scripts, many options and tools are provided in Linux.
In this article, we will show you how to use Zenity to display GUI dialogs in Bash scripts.
Zenity is an open source application for displaying simple GUI in shell script. By displaying the GTK+ dialog box, it makes the script more user-friendly. Zenity is a convenient command line tool for modern shell scripting. Zenity is easy to use and is a cross-platform application.
A sudo user with root privileges.
Zenity is part of the GNOME desktop, and by default, it is installed in most Linux distributions. You can verify the Zenity installation on the system by executing the following command in the terminal.
sudo apt-get install zenity
For RedHat-based systems:
dnf install zenity
For Arch-based systems:
pacman -S zenity
Zenity general options
Zenity can use some general options alone. The general options are –title, –window-icon, –width, –height, –timeout
You can use the — title option to specify a title for the dialog window.
You can use the –window-icon option to specify an icon that will be used as the icon for the “Zenity” dialog box.
Not only that, but you can also set the width and height of the Zenity dialog box by setting the –width and –height options in the Zenity command.
By using the –timeout option, you can set the timeout period of the dialog box, and the dialog box will automatically close.
Zenity message box
You can use simple Zenity commands from the terminal to create some basic and nice GUI message dialogs. There are four message dialogs provided in Zenity.
1. Information dialog
You can create an information dialog box by running the following command in the terminal:
zenity --info --text="Software Update Available." --width=500
2. Error dialog
Execute the following command to display the error dialog
zenity --error --text="Error In Installation" --title="Error" --width=500 --width=200
3. A question dialog
You can easily ask “Yes, yes” by executing the following command:
zenity --question --text="Do you want to reboot pc?" --width=200
You can use the Zenity Question dialog box in a shell script as follows:
#/bin/bashtouch demo.txtif zenity --question --title="Confirm deletion" --text="Are you sure you want to demo.txt file?" --no-wrap then rm demo.txt zenity --info --title="Success" --text="demo.text was removed" --no-wrapfi
4. Warning dialog
zenity --warning --title="Update Available" --text="OS Update required" --width=200
You can use Zenity to display notifications.
zenity --notification --text "Good Morning"
Zenity input box
With Zenity, you can create simple dialogs that take input from the user and display it as standard output.
Here are some input boxes:
The “Calendar” dialog box displays the calendar and returns the date selected by the user as standard output.
Text input dialog
You can use the –entry option to create a text input dialog. This feature is very useful when you want to ask the user to enter some text.
zenity --entry --title "User Info" --text "Please enter your name"
You can use the –list option to create a multi-column dialog box, where you need to enter the data in the list dialog box column by column and row by row. You can use checkboxes or redo boxes with the list dialog box as shown below:
zenity --list --title "Choose Processor" --radiolist --column "ID" --column="Name" 1 AMD 2 Intel
You can also add the –list option to the checkbox to make multiple selections.
zenity --list --title "Packages" --list --text "select packages you want to install" --checklist --column "id" --column "Name" 1 "Google Chrome" 2 "VLC" 3 "Firefox" 4 "GIMP"
Color selection dialog
You can use the –color-selection option to get the color selection value from the user.
zenity --color-selection --show-palette
File selection dialog
You can use the –file-selection option to capture the selected file or directory as standard output. You can also save the file using the –file-selection option.
zenity --file-selection --multiple --filename "/"
You can use the –progress option to create a nice progress bar. This is very useful in many situations where you can show the user an application or script completion progress bar. Below the command with a display download progress bar.
(wget -r --no-parent patch -A.tar.gz https://22.214.171.124/latest/ -P /tmp 2>&1 ) | sed -u 's/.* ([0-9]+%) +([0-9.]+.) (.*)/1n# Downloading at 2/s, ETA 3/' | zenity --progress --title="Downloading File..." --percentage=1 --pulsate
To get more progress options, run the following command in the terminal
For more Zenity options, you can use Man’s enthusiasm with Zenity-Help.
In the above guide, you learned how to write a bash script to display GUI dialogs using Zenity and make Bash scripts more user-friendly. If you have any questions, please feel free to ask me.