Creating dialog boxes with the dialog utility in Linux bash script

May 05, 2021

The dialog utility is used to create a basic-level graphical user interface. We can use this in shell script to create very useful programs.

To install the dialog utility in Debian or Ubuntu Linux, enter the following command:

$ sudo apt-get update
$ sudo apt-get install l dialog

Similarly, enter the following command to install the utility dialog in CentOS or Red Hat Linux:

$ sudo yum install dialog

The typical syntax of the dialog command is as follows:

$ dialog --common-options --boxType "Text" Height Width                                   --box-specific-option

The common-options utility is used to set the background color, title, and so on in dialog boxes.

The option details are as follows:

  • Text: The caption or contents of the box
  • Height: The height of the dialog box
  • Width: The width of the dialog box

Creating a message box (msgbox)

To create a simple message box, we can use the following command:

$ dialog --msgbox "This is a message." 10 50
Dialog box Linux Concept

Creating a message box (msgbox) with a title

Enter the following command to create a message box with the following title:

$ dialog --title "Hello" --msgbox 'Hello world!' 6 20

The option details are as follows:

  • --title "Hello": This will set the title of the message box to "Hello"
  • --msgbox 'Hello world!': This will set the content of the message box to "Hello world!"
  • 6: This will set the height of the message box
  • 20: This will set the width of message box:
Message Box Linux Concept

The message box has a Hello title with content Hello World! It has a single OK button. We can use this message box to inform the user about any events or information. The user will have to press Enter to close this message box. If the content is large for a message box, then the dialog utility will provide the scrolling of the message.

The yes/no box (yesno)

If we need to obtain a yes or no answer from the user, we can use the following options along with the dialog command:

$ dialog --yesno "Would you like to continue?" 10 50
Dialog Box image Linux Concept

We can have the same yes/no dialog box with a title as follows:

$ dialog --title "yesno box" --yesno "Would you like to continue?" 10 50
Dialog Box image Linux Concept

Let’s write the shell script as follows:

dialog --title "Delete file"  
--backtitle "Learning Dialog Yes-No box"  
--yesno "Do you want to delete file "~/work/sample.txt"?" 7 60 
# Selecting "Yes" button will return 0. 
# Selecting "No" button will return 1. 
# Selecting [Esc] will return 255. 
case $result in 
   0)     rm ~/work/sample.txt 
    echo "File deleted.";; 
   1)     echo "File could not be deleted.";; 
   255)   echo "Action Cancelled - Presssed [ESC] key.";; 

Let’s test the following program:

$ chmod +x
$ ./


Dialog box yes no image

The input box (inputbox)

Whenever we want to ask a user to input text via the keyboard, in such situations, the inputbox option is useful. While entering text via the keyboard, we can use keys such as Delete, Backspace, and the arrow cursor keys for editing. If the input text is larger than the input box, the input field will be scrolled. Once the OK button is pressed, the input text can be redirected to a text file:

# dialog  --inputbox  "Please enter something."   10  50 2> /tmp/tempfileVAR=`cat ~/work/output.txt
Input Dialog box image Linux Concept

Let’s write the shell script to create an input box as follows:

>$ $result    # Create empty file 
dialog --title "Inputbox Demo"  
--backtitle "Learn Shell Scripting"  
--inputbox "Please enter your name " 8 60 2>$result 
case $response in 
0)   echo "Hello $name" 
1)   echo "Cancelled." 
255)     echo "Escape key pressed." 
rm $result 

Let’s test the following program:

$ chmod +x
$ ./
dialog box image Linux Concept



"Hello Ganesh Naik"

The textbox (textbox)

If we want to display the contents of the file in a textbox inside the menu created by a dialog, then enter the following command:

$ dialog  --textbox /etc/passwd 10  50
Text Dialog Box Image Linux Concept

We are displaying the /etc/passwd file in the textbox with the previous command.

A password box

Many times, we need a password from the user. In this case, the password should not be visible on the screen. The password box option is perfect for this purpose.

If we want to display an entered password as a string of ****, then we will need to add the --insecure option.

We will need to redirect the inserted password to a file.

Let’s write shell script to receive the password as follows:

# creating the file to store password 
result="output.txt 2>/dev/null" 
# delete the password stored file, if program is exited pre-maturely. 
trap "rm -f output.txt" 2 15 
dialog --title "Password"  
--passwordbox "Please enter password" 10 30 2> $result 
case $reply in 
  0)    echo "You have entered Password :  $(cat $result)";; 
  1)    echo "You have pressed Cancel";; 
  255)    cat $data && [ -s $data ] || echo "Escape key is pressed.";; 

Let’s test the following program:

$ chmod +x
$ ./



You have entered Password :  adcd1234

The checklist box (checklist)

In this case, we can present the user with a choice to select one or multiple options from a list:

# dialog --checklist "This is a checklist" 10 50 2  
"a" "This is one option" "off"  
"b" "This is the second option" "on" 
Checklist Dialog Box Image Linux Concept

The menu box (menu)

Usually, a program or shell script may be required to perform multiple types of tasks. In such cases, the menu box option is very useful. This option will display a list of choices for the user. Then, the user may select an option of their own choice. Our script should execute the desired option.

Each menu has two fields, a tag and an item string. In the next example menu demo, we have tags such as date, calendar, and editor. A description of a tag is called an item string.

Let’s write the shell script to create a menu as follows:

# Declare file to store selected menu option 
# Declare file to store content to display date and cal output 
# trap and delete temp files 
function display_output(){ 
    dialog --backtitle "Learning Shell Scripting" --title "Output" --clear --msgbox "$(<$TEMP_DATA)" 10 41 
function display_date(){ 
    echo "Today is `date` @ $(hostname -f)." >$TEMP_DATA 
    display_output 6 60 "Date and Time" 
function display_calendar(){ 
    cal >$TEMP_DATA 
    display_output 13 25 "Calendar" 
# We are calling infinite loop here 
while true 
# Show main menu 
dialog --help-button --clear --backtitle "Learn Shell Scripting"  
--title "[ Demo Menubox ]"  
--menu "Please use up/down arrow keys, number keysn 
1,2,3.., or the first character of choicen 
as hot key to select an option" 15 50 4  
Calendar "Show the Calendar"  
Date/time "Show date and time"  
Editor "Start vi editor"  
Exit "Terminate the Script" 2>"${RESPONSE}" 
# Start activity as per selected choice 
case $menuitem in 
    Calendar) display_calendar;; 
    Date/time) display_date;; 
    Editor) $vi_editor;; 
    Exit) echo "Thank you !"; break;; 
# Delete temporary files 
[ -f $TEMP_DATA ] && rm $TEMP_DATA 
[ -f $RESPONSE ] && rm $RESPONSE 

Let’s test the following program:

$ chmod +x
$ ./


Menu Dialog Box Image Linux Concept

The radiolist box (radiolist)

If you want the user to select only one option out of many choices, then radiolist is a suitable option:

# dialog --radiolist  "This is a selective list, where only one  
option can be chosen" 10 50 2  
"a" "This is the first option" "off"  
"b" "This is the second option" "on" 

Radio buttons are not square but round, as can be seen in the following screenshot:

Redio Dialog Box Image Linux Concept

The progress meter box (gauge)

The progress meter displays a meter at the bottom of the box. This meter indicates the percentage of the process completed. New percentages are read from standard input, one integer per line. This meter is updated to reflect each new percentage.

Let’s write the shell script to create a progress meter as follows:

declare -i COUNTER=1 
    while test $COUNTER -le 100 
            echo $COUNTER 
            sleep 1 
    } |  dialog --gauge  "This is a progress bar"  10 50 0

Let’s test the following program:

$ chmod +x
$ ./


Progress Bar Dialog Box Image Linux Concept

Customization of dialog with the configuration file.

We can customize dialog using the ~/.dialogrc configuration file. The default file location is $HOME/.dialogrc.

To create the .dialogrc configuration file, enter the following command:

$ dialog --create-rc ~/.dialogrc

We can customize the output of the dialog utility by changing any of the configuration parameters defined in the .dialogrc file.

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur


Submit a Comment

Your email address will not be published. Required fields are marked *

eight + eight =