How to display GUI dialogs in bash script using Zenity

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.

Prerequisites

A sudo user with root privileges.

Install Zenity

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.

zenity --version


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.
 

  • Information
  • Error
  • Question
  • Warning

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


Display info text dialog using bash script

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


Show error dialog using bash script

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

Dialog boxes in bash
You can use Zenity Question Dialog in shell script like this:

#/bin/bash
touch demo.txt
if 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-wrap
fi

4. A Warning Dialog Box

zenity --warning --title="Update Available" --text="OS Update required" --width=200

Bash show warning dialog box

Notification

You can display Notification using Zenity.

zenity --notification --text "Good Morning"

Show desktop notifications in GNOME using bash scripts

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

Calendar Dialog box display a calendar and return user's selected date as standard output.

zenity --calendar
Output:
17/04/21

Calendar input from bash script

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"

List Dialog

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

List Dialog box

You can also give checkbox with --list option for multiple selection.

Checkbox options with bash

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"
Output
Google Chrome|VLC|Firefox

Color Selection Dialog

You can use --color-selection option to take color selection value from the user.

zenity --color-selection --show-palette

Bash script color picker

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 "/"

Open file select box from bash

Progress Dialog

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://157.245.99.119/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

zenity --help-progress

For more Zenity options you can use man Zenity and Zenity --help.

Conclusion

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.

Share this page:

Suggested articles

1 Comment(s)

Add comment

Comments

By: nandelbosc at: 2021-04-23 08:48:58

Clear and easy. Thank's