Creating dialog boxes with the Dialog Tool in Linux

The Dialog tool is a command-line utility in Linux that allows developers to create interactive dialog boxes within shell scripts. These dialog boxes provide a user-friendly interface for displaying information, receiving input, and making selections, significantly enhancing the usability of command-line applications.

Dialog boxes serve as an excellent bridge between the command line and graphical user interfaces, offering a text-based visual interaction that is both accessible and functional. This makes them particularly valuable for system administration scripts and user-interactive applications.

Installing Dialog Tool

Before creating dialog boxes, ensure that the Dialog tool is installed on your Linux system. Check if it's already installed by running

dialog --version

If Dialog is not installed, use your package manager to install it. For Debian-based systems

sudo apt-get update
sudo apt-get install dialog

For Red Hat-based systems, use

sudo yum install dialog

Verify the installation by running dialog --version again to see the version information.

Creating a Simple Message Box

Let's start with a basic message box that displays information to the user. Create a shell script file called dialog_example.sh

#!/bin/bash

dialog --msgbox "Welcome to Dialog tool tutorial!" 10 40

The --msgbox parameters are

  • "Welcome to Dialog tool tutorial!" The message text

  • 10 Height of the dialog box (lines)

  • 40 Width of the dialog box (characters)

Make the script executable and run it

chmod +x dialog_example.sh
./dialog_example.sh

Getting User Input

Input boxes allow users to enter text data. Create an input dialog script

#!/bin/bash

dialog --inputbox "Please enter your name:" 10 40 2> /tmp/input.txt

# Read the user input
name=$(cat /tmp/input.txt)

# Display confirmation
dialog --msgbox "Hello, $name!" 8 30

# Clean up temporary file
rm -f /tmp/input.txt

The 2> /tmp/input.txt redirects the user's input to a temporary file. The input can then be read and used within the script for further processing.

Creating Selection Menus

Menu dialogs present users with multiple options to choose from

#!/bin/bash

dialog --menu "Select your favorite programming language:" 15 40 4 \
1 "Python" \
2 "JavaScript" \
3 "C++" \
4 "Java" 2> /tmp/selection.txt

# Process the selection
choice=$(cat /tmp/selection.txt)
case $choice in
    1) language="Python" ;;
    2) language="JavaScript" ;;
    3) language="C++" ;;
    4) language="Java" ;;
esac

dialog --msgbox "You selected: $language" 8 30
rm -f /tmp/selection.txt

The menu parameters include the number of items (4) followed by pairs of item numbers and descriptions.

Advanced Dialog Types

Checklist Dialogs

Checklists allow multiple selections

dialog --checklist "Select your skills:" 15 40 4 \
1 "Linux" off \
2 "Programming" on \
3 "Networking" off \
4 "Database" on 2> /tmp/checklist.txt

Form Inputs

Forms collect multiple pieces of information in a structured layout

dialog --form "Enter user details:" 15 50 3 \
"Name:" 1 1 "" 1 10 20 0 \
"Email:" 2 1 "" 2 10 30 0 \
"Phone:" 3 1 "" 3 10 15 0 2> /tmp/form.txt

File Selection

File selection dialogs enable browsing and selecting files

dialog --fselect "/home/" 15 60 2> /tmp/file.txt

Common Dialog Options

Option Purpose Usage
--title Sets dialog title --title "My Dialog"
--clear Clears screen after dialog --clear
--colors Enables color markup --colors
--no-cancel Removes Cancel button --no-cancel

Error Handling

Dialog returns different exit codes based on user actions. Handle these in your scripts

#!/bin/bash

dialog --yesno "Do you want to continue?" 8 30
response=$?

case $response in
    0) echo "User selected Yes" ;;
    1) echo "User selected No" ;;
    255) echo "User pressed Escape" ;;
esac

Conclusion

The Dialog tool transforms command-line scripts into interactive applications with user-friendly interfaces. By mastering message boxes, input forms, menus, and file selectors, you can create sophisticated text-based user interfaces that greatly improve the user experience of shell-based applications.

Updated on: 2026-03-17T09:01:39+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements