Bash display dialog boxes

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← Getting information about your systemHomedialog customization with configuration file →
  • The dialog command allows you to display a variety of questions or display messages using dialog boxes from a shell script.
  • Use the dialog utility for creating TTY (terminal) dialog boxes.

Install dialog command

Type the following command to install the dialog command line utility under Debian or Ubuntu Linux:

sudo apt-get update
sudo apt-get install dialog

Type the following command to install the dialog command line utility under CentOS or Redhat Linux:

yum install dialog

Syntax

The syntax is as follows:

dialog --common-options --boxType "Text" Height Width --box-specific-option
  • --common-options are used to set dialog boxes background color, title, etc.
  • All dialog boxes have at least three parameters:
    • "Text" : The caption or contents of the box.
    • height : The height of the dialog box.
    • width : The width of the dialog box.

Your first dialog

dialog command in action

Type the following command at a shell prompt:

dialog --title "Hello" --msgbox 'Hello world!' 6 20
  1. A message box is displayed on the screen with a single OK button.
  2. You can use this dialog box to display any message you like.
  3. After reading the message, the user can press the ENTER key so that dialog will exit and the calling shell script can continue its operation.
  4. If the message is too large for the space, dialog may allow you to scroll it. In this case, a percentage is shown in the base of the widget.
  5. On exit, no text is written to dialog’s output. Only an "OK" button is provided for input, but an ESC exit status may be returned.

Understanding dialog options

  • --title "Hello"  : Set a "title string" (caption) to be displayed at the top of the dialog box. In this example, set a title string to "Hello".
  • --msgbox 'Hello world!' : Create a message box with "Hello world!" message.
  • 6  : Set the height of the msgbox box.
  • 20 : Set the width of the msgbox box.

Setting backtitle

You can set a backtitle string to be displayed on the backdrop, at the top of the screen using the --backtitle "Title" syntax:

dialog --backtitle "System Information" \
--title "About" \
--msgbox 'This is an entirely open source software.' 10 30

Positioning the box

The --begin y x option can be used to to set the position of the upper left corner of a dialog box on the screen.

dialog --begin 10 30 --backtitle "System Information" \
--title "About" \
--msgbox 'This is an entirely open source software.' 10 30

Where,

  • --begin 10 30: 10 is y i.e. horizontal position and 30 is vertical position.
Dialog command with y x position and backtitle


Common dialog boxes and their options

Box options:

  --calendar     <text> <height> <width> <day> <month> <year>
  --checklist    <text> <height> <width> <list height> <tag1> <item1> <status1>...
  --dselect      <directory> <height> <width>
  --editbox      <file> <height> <width>
  --fselect      <filepath> <height> <width>
  --gauge        <text> <height> <width> [<percent>]
  --infobox      <text> <height> <width>
  --inputbox     <text> <height> <width> [<init>]
  --inputmenu    <text> <height> <width> <menu height> <tag1> <item1>...
  --menu         <text> <height> <width> <menu height> <tag1> <item1>...
  --msgbox       <text> <height> <width>
  --passwordbox  <text> <height> <width> [<init>]
  --pause        <text> <height> <width> <seconds>
  --progressbox  <height> <width>
  --radiolist    <text> <height> <width> <list height> <tag1> <item1> <status1>...
  --tailbox      <file> <height> <width>
  --tailboxbg    <file> <height> <width>
  --textbox      <file> <height> <width>
  --timebox      <text> <height> <width> <hour> <minute> <second>
  --yesno        <text> <height> <width>
← Getting information about your systemHomedialog customization with configuration file →