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
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.