Adding dialog boxes to shell scripts
Command Line – dialog
Create dialog boxes with checkboxes, progress bars, and many other features that users may find helpful when working at the command line.
Many Bash scripts do not need interfaces. They are run by admins, who are comfortable at the command line. However, if a script is used by everyday users, it may be more friendly if it uses a dialog box for input and messages. For over 25 years, a leading command for boxes has been dialog
[1], which can be called from a script so that users can enter input in an ncurses interface. It then returns them to the script when they exit.
The command structure of dialog
is somewhat unusual (Listing 1). In addition to the appearance of options in two separate places, note the quotation marks around the text, and the order of height, width, and other box type options. Height is expressed as the number of lines and width as the number of monospaced characters.
Listing 1
Command Structure for dialog
The design of the resulting box varies with the options selected. However, it will always have text and may have buttons (such as Yes, No, Help, or OK) or radio buttons for making selections. It may also have a back title for easy identification. When necessary, the box can be navigated by using arrow keys, and it is not supported by a mouse (Figure 1). Once a selection is made, then generally the script will continue with if/then/else statements that correspond to each selection.
Generally, the easiest way to design a box is to open another prompt and run the basic dialog
command repeatedly until you have a structure ready to add to the script. Use the Esc key to return to the prompt after the resulting box is displayed. Unless the background for the dialog box matches the background color for the terminal, you will see conflicting colors after returning to the prompt unless you enter the clear command or a sufficient number of new commands.
Using Box Options
It makes sense to begin with the available types of boxes, one of which must always be used in a dialog command. All boxes can use the self-explanatory text, height, and width options. Text appears after the box type in quotation marks, while height and width options follow, expressed in lines and characters respectively.
Other options must follow the width. Available options depend on the type of box, as shown in Table 1.
Table 1
Selected Boxes and Their Options
--background tail |
Like |
|
--calendar |
Displays calendar |
--day --month --year: For current date |
--checklist |
A scrolling list from which to select multiple items |
--list-height: Tag item status format for default choices (e.g., |
--dselect |
Selects a directory from a list |
--filepath |
--editbox |
Allows editing of an existing file |
--filepath |
--gauge |
A progress bar |
--percent: Displays completion percentage |
--info |
A message display |
|
--input |
A text entry field for answering questions |
|
--menu |
A scrolling list from which to select one item |
--menuheight: Tag item status format for default choices (e.g., |
--message |
OK button |
|
--passwordbox |
For password entry |
|
--pause |
Shows meter for time paused |
--seconds |
--radiolist |
Shows radio boxes for selection |
Tag item status format for default choices ( |
--tail |
An auto-updating viewer for the end of files |
|
--timebox |
Selects time |
--hour --minute --seconds |
--text |
A scrollable text box |
|
--yes/no |
Yes and No answer buttons |
|
All boxes can use text, height, and width. Options can be completed to dimension, time, filename, or some other variable as needed. |
---|
A simple message box (Figure 2) would use a command like:
dialog --title "Choose" --msgbox 'Invalid option. Choose again' 6 20
A more complicated example is a checklist, which asks for a selection (see Listing 2 and Figure 3).
Listing 2
Checklist
Listing 2 shows items prefaced with an identifying tag and followed by a status for the default display, as well as the end of line markers.
Other boxes have similar structures, modified by any common command options. For example, the message box given above might have a help button added to it with the addition of a --help button
option (Figure 4).
For a complete description of each option, see the man page. Some options have restrictions, most of which are logical enough when you stop to think. For example, a help button can only be added to checklist, radio list, and menu boxes. There would be no point to adding a help button to a message box, for instance, which requires no user interaction.
Screenshots of other boxes are available online [2].
Common Dialog Options
Common dialog options affect the look and general functionality of the command. --ascii-line
replaces the ncurses widgets used to create a box with plus and minus signs instead (Figure 5). Ordinarily, a box is centered on the screen, but you can also use --begin Y X
to set the vertical and horizontal coordinates for the upper left corner of the box. You can also set the dimensions of a box with --aspect WIDTH/HEIGHT
. With --scrollbar
, a scrollbar is added to the box, while --no-shadow
suppresses the default shadow to the right and bottom of the box – an option which gives ncurses a flatter but more modern-looking appearance.
--color
expresses settings prefaced by a \Z
. It uses the numbers 0-7
to choose ANSI colors: black, red, green, yellow, blue, magenta, cyan, and white – in that order. However, contrary to what you might expect from the name, --color
also sets font weights and effects: b
sets bold, and B
turns off bold, while u
turns on underlining and U
turns it off. The settings are cumulative, so Z\u\3
produces green, underlined text. To restore default settings, use \Zn
.
Many of the common options have to do with how the buttons in a box are used. Options like --no-cancel
suppress the display of a specific button altogether. Others, like --help button
add a button to types of boxes that could use one; a gauge box, for example, would have no use for a Help button, since there is no interaction. Others, like --exit-label STRING
, --ok-label STRING
, --no-label STRING
, and --yes-label STRING
replace the standard button titles, making the buttons more versatile. In boxes like menus, checklists, and radio lists that require a selection of a list of items, you can also use --item-help
to add help for each item, and --default-item STRING
to select which choices are selected by default.
Still other items affect what happens after a dialog displays. For example, --stdout
prints the result of a dialog interaction to standard input and --stderr
to standard error, which may simplify scripting. Script writers may also use --sleep SECONDS
to pause a script after it processes input from a dialog box or --and-widget
to force dialog to move to the next box after the current one is processed without resuming the script.
dialog
's options total in the dozens, and setting up the command structure can be laborious. For this reason, you may want to run dialog --create-rc FILE
in your home directory to create a default look and function for the command. The file uses the current settings of your current terminal. Although you probably want to leave the top of the line untouched, there are many choices that are self-explanatory enough to edit. This file can be overridden by options entered at the command line, but can save considerable time if you use dialog
regularly (Figure 6).
Alternatives to Dialog
While dialog
is the most common option for adding partial interfaces to scripts, both whiptail
[3] and zenity
[4] offer a similar, if less complete command structure. However, zenity
's man page is perhaps needlessly complex, dividing options into 10 different categories that at first make it difficult to see the overall command structure. Moreover, zenity
uses GTK+ rather than ncurses.
In addition, Xdialog
[5] is intended as a drop-in replacement for dialog
, and is referenced in dialog
's man pages to mention a few minor differences. However, dialog
remains the most extensive and more commonly used. As primitive as ncurses may appear to modern users, it is generally adequate for the purposes of most scripts.
Infos
- dialog: https://invisible-island.net/dialog/
- More dialog screenshots: https://linuxgazette.net/101/sunil.html
- whiptail: https://en.wikibooks.org/wiki/Bash_Shell_Scripting/Whiptail
- zenity: https://wiki.gnome.org/action/show/Projects/Zenity
- Xdialog: https://linux.die.net/man/1/xdialog
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.