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


<img alt="Display info text dialog using bash script" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/info.png607f14aa72bd8.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="403" loading="lazy" src="data:image/svg xml,” width=”750″>

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


<img alt="Show error dialog using bash script" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/screenshot_from_2021-04-16_15-25-36.png607f14abea425.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="220" loading="lazy" src="data:image/svg xml,” width=”750″>

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

<img alt="Dialog boxes in bash" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/screenshot_from_2021-04-16_16-29-55.png607f14ad5a809.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="225" loading="lazy" src="data:image/svg xml,” width=”750″>

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

<img alt="Bash show warning dialog box" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/screenshot_from_2021-04-16_15-49-06.png607f14adac057.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="181" loading="lazy" src="data:image/svg xml,” width=”750″>

Notification

You can display Notification using Zenity.

zenity --notification --text "Good Morning"

<img alt="Show desktop notifications in GNOME using bash scripts" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/screenshot_from_2021-04-16_16-58-05.png607f14aee2c18.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="116" loading="lazy" src="data:image/svg xml,” width=”750″>

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.Advertisement

zenity --calendar
Output:

17/04/21

<img alt="Calendar input from bash script" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/cal.png607f14af43087.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="414" loading="lazy" src="data:image/svg xml,” width=”471″>

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

<img alt="List Dialog box" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/radiobox.png607f14afa1bf5.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="251" loading="lazy" src="data:image/svg xml,” width=”750″>

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

<img alt="Checkbox options with bash" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/list-checkbox.png607f14b029d3e.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="218" loading="lazy" src="data:image/svg xml,” width=”750″>

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

<img alt="Bash script color picker" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/color.png607f14b07e008.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="363" loading="lazy" src="data:image/svg xml,” width=”591″>

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 "https://www.howtoforge.com/"

<img alt="Open file select box from bash" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/04/echo/file.png607f14b1ba136.jpg" ezimgfmt="rs rscb3 src ng ngcb3" height="429" loading="lazy" src="data:image/svg xml,” width=”750″>

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.Advertisement

(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.] .) (.*)/1n# 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.