profile for Kells1986 at Stack Overflow, Q&A for professional and enthusiast programmers

Thursday, 12 September 2013

Use Dialog to Add a Simple GUI to a Bash Script

I've been looking for a tool to create a GUI for my scripts recently. I wanted to find something that wouldn't introduce too much more coding but add a professional front-end.

I came across dialog, it's a tool that you can use in your scripts to do just what I wanted to do. Here is a simple "Hello World!" code that you can use:

dialog --title 'Message' --msgbox 'Hello, world!' 5 20

You can create menu pages, checklists, yes-no boxes, input-boxes and much more. 

To install it, all you need to do on Ubuntu is sudo apt-get install dialog. You can also install it on Mac OSX - if you install home brew - using brew install dialog.

There are a couple of things you need to know about dialog to get a simple app running. You get data back from the GUI by either reading stderr, or from dialog's return value. For instance the dialog --menu page will return 0 if the user presses Ok, 1 if the user presses Cancel, or 255 is Esc is pressed. If the user selects Ok, the number of the selected item will be written to stderr.

This is best illustrated by a very simple example app. This script doesn't have much functionality but should illustrate the principles adequately:



while [[ 1 ]]; do
dialog --cancel-label "Quit" --menu "Choose one:" 10 30 5 1 "Enter Name" 2 "Something Else" 2>$reader

if [[ $retval != 0 ]]; then
exit 0

choice=$(cat $reader)

if [[ $choice == 1 ]]; then

dialog --inputbox "Enter your name:" 8 40 2>$reader
name=$(cat $reader)
dialog --infobox "Your name is ${#name} characters long" 10 50
sleep 2


dialog --infobox "Unfortunately I can't think of another example." 10 50
sleep 2


If you run this script you will see a menu page like this:

Here you can see that stderr - file descriptor number 2 - is redirected to a file in /tmp. That is how the menu and input boxes are read. The return value of the menu page is checked to see if the user wants to quit. On the main page it should be possible to cancel or press escape to quit the script.

Another compatible library for developing GUIs is Xdialog. It is syntactically similar to dialog, but shows X windows instead.


  1. Also zenity for a GTK equivalent

  2. When I read the post title on Reddit I was mildly excited for a pure bash solution rather than a call to a program that makes curses interfaces or similar. A neat package, to be sure, but this one I already knew about and is hardly innovative or clever for someone to use rather than write.