How to Show Dialog Box from Bash Shell Script with Examples

April 10, 2015 | By in SHELL SCRIPTS
| Reply More

This tutorial will give couple of examples on how to use utilities like zenity and whiptail in your Bash shell script to provide message / dialog box . With these utilities your script will be able to inform user about the current state of execution, or give an ability to interact. Difference between these two utilities is the way they are displaying message box or dialog. Zenity uses GTK toolkit for creating graphical user interfaces, while whiptail creates message boxes inside terminal window.

Zenity Tool

To install zenity on Ubuntu run:

sudo apt-get install zenity

Since commands for creating message boxes or dialogs with zenity are pretty self explanatory, we will provide you with couple of examples.

Creating information box

zenity --info --title "Information Box" --text "This should be information" --width=300 --height=200

Screenshot-Information Box

Creating Yes/No dialog

zenity --question --text "Do you want this?" --ok-label "Yeah" --cancel-label="Nope"


Creating entry box and storing value in variable

a=$(zenity --entry --title "Entry box" --text "Please enter the value" --width=300 --height=200)
echo $a

Screenshot-Entry box

After entering it, value will be stored in $a variable.

Here is the working example which takes user's first name, last name and age and displays it.

# This script will ask for couple of parameters
# and then continue to work depending on entered values

# Giving the option to user
zenity --question --text "Do you want to continue?"

# Checking if user wants to proceed
[ $? -eq 0 ] || exit 1

# Letting user input some values
FIRSTNAME=$(zenity --entry --title "Entry box" --text "Please, enter your first name." --width=300 --height=150)
LASTNAME=$(zenity --entry --title "Entry box" --text "Please, enter your last name." --width=300 --height=150)
AGE=$(zenity --entry --title "Entry box" --text "Please, enter your age." --width=300 --height=150)

# Displaying entered values in information box
zenity --info --title "Information" --text "You are ${FIRSTNAME} ${LASTNAME} and you are ${AGE}(s) old." --width=300 --height=100

Here are the screenshots of previous script.


First box


Example1-Entry box-1

Entry box


Example1-Entry box-2

Entry box


Example1-Entry box-3

Entry box



Information box


Don't forget refer some of the useful zenity options that might help you.

Whiptail Tool

To install whiptail on Ubuntu run

sudo apt-get install whiptail

Commands for creating message box/dialog with whiptail are also self explanatory, so we will provide you with just couple of basic examples.

Creating message box

whiptail --msgbox "This is a message" 10 40


Creating Yes/No dialog

whiptail --yes-button "Yeah" --no-button "Nope" --title "Choose the answer" --yesno "Will you choose yes?" 10 30


Creating entry box with default value

whiptail --inputbox "Enter your number please." 10 30 "10"


One thing to be aware of when trying to use entered value is that whiptail uses stdout for displaying dialog, and stderr for value output. That way, if you use var=$(...) you wont see dialog box at all, and wont get the entered value. Solution is to switch stdout and stderr. To do that just add 3>&1 1>&2 2>&3 at the end of the whiptail command. Same would be with any whiptail command which you want to use to get some entered value.

Creating menu dialog

whiptail --menu "This is a menu. Choose an option:" 20 50 10 1 "first" 2 "second" 3 "third"


Here is a shell script that asks user to enter a path to a folder and then outputs it's size.


# Since whiptail has to use stdout to display dialog, entered value will
# be stored in stderr. To switch them and get the value to stdout you must
# use 3>&1 1>&2 2>&3
FOLDER_PATH=$(whiptail --title "Get the size of folder" \
--inputbox "Enter folder path:" \
10 30 \
"/home" \
3>&1 1>&2 2>&3)

if [ -d $FOLDER_PATH ]
size=$(du -hs "$FOLDER_PATH" | awk '{print $1}')
whiptail --title "Information" \
--msgbox "Size of ${FOLDER_PATH} is ${size}" \
10 40
elif [ -f $FOLDER_PATH ]
whiptail --title "Warning!!!" \
--msgbox "The path you entered is a path to a file not a folder!" \
10 40
whiptail --title "Error!!!"
--msgbox "Path you entered is not recognized. Please try again" \
10 40

Here are the screenshots from previous example:


Entry box


Information box

If you are working in terminal , manual pages are always available.


Choosing the right tool for displaying dialogs will depend on whether you expect your script to be run on desktop machine or server machine. Desktop machine users mostly use window environment and will possibly be able to run the script and interact with appearing windows. However, if you are expecting that the user is someone on server machine, you might want to play it safe and use whiptail or any other utility that will display dialogs in plain terminal window.


Share This :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.