dialog
Display user-friendly dialog boxes from shell scripts
TLDR
Display a message
Prompt the user for text
Prompt the user for a yes/no question
Display help
SYNOPSIS
dialog [common options] <box-type> <text> <height> <width> [box-specific arguments]
Common Options include:
--backtitle <string>
--title <string>
--clear
--no-shadow
--stdout | --stderr
Box Types include:
--msgbox
--yesno
--inputbox
--textbox
--menu
--checklist
--radiolist
--fselect
--gauge
PARAMETERS
--backtitle <string>
Sets a title to be displayed on the screen background, usually at the top.
--title <string>
Sets a title for the dialog box itself, displayed at the top of the box.
--clear
Clears the screen before displaying the dialog box.
--no-shadow
Disables the shadow effect around the dialog box.
--stdout
Redirects dialog's output (user input/selection) to standard output. By default, this output goes to standard error (or /dev/tty).
--stderr
Directs dialog's output (user input/selection) to standard error. This is the default behavior.
--msgbox <text> <height> <width>
Displays a simple message box with an 'OK' button. It pauses until the user presses OK.
--yesno <text> <height> <width>
Displays a question with 'Yes' and 'No' buttons. Returns 0 for Yes, 1 for No.
--inputbox <text> <height> <width> [<init>]
Displays an input box, allowing the user to enter a single line of text. The optional <init> provides default text.
--textbox <file> <height> <width>
Displays the contents of a specified text file. Allows scrolling if the content exceeds the box size.
--menu <text> <height> <width> <menu_height> [<tag> <item>]...
Displays a menu from which the user can select one item. Each item has a unique <tag> and a descriptive <item>.
--checklist <text> <height> <width> <list_height> [<tag> <item> <status>]...
Displays a list with checkboxes, allowing multiple selections. <status> can be 'on' or 'off'.
--gauge <text> <height> <width> [<percent>]
Displays a progress bar. The percentage can be provided initially or read from standard input.
--fselect <filepath> <height> <width>
Displays a file selection box, allowing the user to browse directories and select a file.
--timebox <text> <height> <width> [<hour> <minute> <second>]
Displays a time selection box, allowing the user to pick a time.
--calendar <text> <height> <width> [<day> <month> <year>]
Displays a calendar, allowing the user to pick a date.
--nocancel
Hides the 'Cancel' button, forcing the user to make a choice (e.g., Yes/No for a yesno box).
DESCRIPTION
dialog is a program that allows you to present a variety of user-friendly dialog boxes from shell scripts. It utilizes the ncurses library to provide a terminal-based graphical user interface (TUI). This enables script developers to create interactive prompts, menus, and information displays without relying on a full graphical environment.
Common uses include asking yes/no questions, getting text input, displaying messages, showing progress bars, selecting items from lists, and browsing files. dialog directs the user's input (e.g., entered text, selected menu items) to standard output, while error messages and the dialog box itself are drawn on standard error (unless redirected). Its return code indicates the user's action (e.g., OK, Cancel, Yes, No, ESC). This makes it a powerful tool for enhancing the interactivity and user experience of command-line scripts.
CAVEATS
- Terminal Dependency: dialog requires a terminal that supports ncurses. It is not suitable for use in non-interactive scripts or environments without a proper terminal emulator.
- Output Redirection: By default, dialog draws its visual output (the box) to standard error and prints user input/selection to standard output. This often necessitates careful redirection (e.g.,
2>&1 >/dev/tty
) in shell scripts to capture output correctly while displaying the dialog. - Limited GUI: While providing a TUI, it offers only basic, grid-based layouts. It cannot create complex graphical interfaces or widgets found in modern desktop environments.
- Exit Status: The program's exit status is crucial for script logic (e.g., 0 for OK/Yes, 1 for Cancel/No). An exit status of 255 typically indicates the user pressed the ESC key.
- Shell Quoting: Text arguments containing spaces or shell metacharacters require careful quoting to be passed correctly to dialog.
CAPTURING USER INPUT
When using box types like --inputbox, --menu, --checklist, or --radiolist, dialog prints the user's selection or entered text to standard output. Scripts typically capture this output using command substitution.
Example:RESPONSE=$(dialog --inputbox "Enter your name:" 10 40 2>&1 >/dev/tty)
The common redirection 2>&1 >/dev/tty
ensures that the dialog box itself is drawn on the terminal (typically /dev/tty
), while dialog's standard output (the user's response) is captured by the shell variable RESPONSE
.
HISTORY
The dialog program was originally written by Lars Wirzenius and became a staple utility in Linux and Unix-like systems, often distributed as part of the ncurses library. Its development in the early 1990s addressed the need for interactive command-line interfaces for shell scripts, bridging the gap between simple command-line tools and full graphical applications. Over decades, it has evolved with new box types and options, maintaining its core purpose of providing a portable and robust method for user interaction within the terminal environment.