How to display GUI dialogs in bash script using Zenity
On this page
- Install Zenity
- Zenity General options
- Zenity Message Box
- Zenity Input Boxes
We all know that Linux bash scripts are a real strength of Linux. Often we want to display a graphical user interface (GUI) in our scripts to make interaction with users easier. GUI makes any script more user-friendly and beautiful.
For GTK in shell scripts, there are many options and tools available 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 scripts. It makes scripts more user-friendly by displaying GTK+ dialogs. Zenity is a handy command-line tool for modern shell scripting. Zenity is easy to use and a cross-platform application.
A sudo user with root privileges.
Zenity is a part of the GNOME desktop and by default, it is installed on most of the Linux distributions. You can verify Zenity installation on your system by executing the following command into the terminal.
If Zenity not installed on your system then you can install it using Apt, dnf or pacman command.
For Ubuntu/Debian based systems:
sudo apt-get install zenity
For RedHat based systems :
dnf install zenity
For Arch based systems:
pacman -S zenity
Zenity General options
There are some General options that can be used with Zenity independently. General options are --title, --window-icon, --width, --height, --timeout
You can use ---title option to specify a title for the dialogue window.
You can use --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 set width and height for the Zenity Dialog Box by setting --width and --height options in Zenity command.
By using the --timeout option You can set a timeout of the dialogue, after that dialog box automatically closed.
Zenity Message Box
You can create some basic and nice GUI message dialog using simple Zenity command from the terminal.
Four type of Message dialog available in Zenity.
1. An Information Dialog Box
You can create information Dialog Box by running the following command in terminal:
zenity --info --text="Software Update Available." --width=500
2. An error Dialog Box
Execute following command to display error dialog box
zenity --error --text="Error In Installation" --title="Error" --width=500 --width=200
3. A Question Dialog Box
You can easily ask for Yes of no question by executing the following command:
zenity --question --text="Do you want to reboot pc?" --width=200
if zenity --question --title="Confirm deletion" --text="Are you sure you want to demo.txt file?" --no-wrap
zenity --info --title="Success" --text="demo.text was removed" --no-wrap
4. A Warning Dialog Box
zenity --warning --title="Update Available" --text="OS Update required" --width=200
You can display Notification using Zenity.
zenity --notification --text "Good Morning"
Zenity Input Boxes
Using Zenity you can create simple dialogs boxes that take input from the user and display it as standard output.
Here are some input boxes:
Calendar Dialog box display a calendar and return user's selected date as standard output.
Text Entry Dialog
You can use --entry option to create a text entry dialog. It is very useful when you want to ask the user to input some text.
zenity --entry --title "User Info" --text "Please enter your name"
You can use the --list option to create a multi-column dialogue, Here you need to enter Data for list dialog column by column, row by row. You can use checkbox or rediobox with list dialog box as shown below:
zenity --list --title "Choose Processor" --radiolist --column "ID" --column="Name" 1 AMD 2 Intel
You can also give checkbox with --list option for multiple selection.
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 --color-selection option to take color selection value from the user.
zenity --color-selection --show-palette
File Selection Dialog
You can use --file-selection option to capture selected files or directories as standard output. You can also save file using --file-selection option.
zenity --file-selection --multiple --filename "/"
You can create a nice progress bar using --progress option. This is useful many times, where you want to show your application or script completion progress bar to the user.
Below command with show you download progress bar.
(wget -r --no-parent patch -A.tar.gz http://22.214.171.124/latest/ -P /tmp 2>&1 ) | sed -u 's/.* \([0-9]\+%\)\ \+\([0-9.]\+.\) \(.*\)/\1\n# Downloading at \2\/s, ETA \3/' | zenity --progress --title="Downloading File..." --percentage=1 --pulsate
For more progress option run following command into terminal
For more Zenity options you can use man Zenity and Zenity --help.
In the above guide, you learned how tyou can write a bash script to display GUI dialogs using Zenity and make Bash script more user-friendly. Feel free to ask me if you have any questions.