Common dialog
Previously we have looked at various ways of making scripts interactive with
read and select.
You can use the dialog package if you want something a bit fancier.
It allows you to create various dialog boxes from your scripts.
Installation
Ubuntu / Debian
sudo apt install dialog
Arch Linux
sudo pacman -S dialog
Usage
To show a simple message:
dialog --msgbox "Hello World" 6 20
Where 6 and 20 is the height and width of the dialog.
A title can also be set.
dialog --title "Lorem Ipsum" \
--msgbox "Incidunt ut ullam laudantium. Illum enim accusantium fugit ipsa. Molestiae quia quia sit eaque ut harum. Nemo sunt et cum error doloremque animi aut consequatur." \
10 40
Notice \ is used escape newline char, so the command can span several lines.
Some of the text is still visible after the dialog has been closed.
This can be fixed with the --clear flag.
dialog --title "Lorem Ipsum" --clear \
--msgbox "Incidunt ut ullam laudantium. Illum enim accusantium fugit ipsa. Molestiae quia quia sit eaque ut harum. Nemo sunt et cum error doloremque animi aut consequatur." \
10 40
Input
We can also make dialog boxes that allows the user to type input.
dialog --title "Name" --clear --inputbox "Please enter your name:" 10 40
You might be wondering, how you can reference the text that was types into an
inputbox.
It simply goes to stderr so you can redirect it to a file.
dialog --title "Name" --clear --inputbox "Please enter your name:" 10 40 2> name
Now you got a file named “name” with whatever the user typed.
Notice 2> means redirect stderr.
If you don’t want to use any intermediate files, then we can pass --stdout
flag.
name=`dialog --stdout --title "Name" --clear --inputbox "Please enter your name:" 10 40`
Yes/No
Great for confirming an action before proceeding.
dialog --title "Proceed" --clear --yesno "Do you really want to proceed?" 10 40
Check the exit code for answer (echo $?) where 0 is “yes” and 1 is “no”.
Other types of boxes
Password
The package supports many different kinds of dialogs. Let’s look at some.
dialog --title "Password" --insecure --clear --passwordbox "Please enter your password for sudo:" 10 40
Or with stars *:
dialog --title "Password" --insecure --clear --passwordbox "Please enter your password for sudo:" 10 40
Menu
dialog --clear --title "You favorite distro" "$@" \
--menu "What is your favorite Linux distribution?\n\n\
Choose the distro you like:" 20 50 4 \
"Ubuntu" "Very popular" \
"Mint" "Often recommended to beginners" \
"Bazzite" "Great for gaming" \
"Arch Linux" "Simple and customizable" \
"NixOS" "Unified configuration"
Checklist
dialog --title "Which are fruits?" "$@" \
--checklist "HR have asked you to identify objects.\n\
Your answer might be used to replace humans with AI.\n\
Press SPACE to toggle an option on/off. \n\n\
Which of the following are fruits?" 20 61 5 \
"Apple" "It's an apple." off \
"Dog" "No, that's not my dog." ON \
"Orange" "Yeah, that's juicy." off \
"Chicken" "Normally not a pet." off \
"Cat" "No, never put a dog and a cat together!" oN \
"Fish" "Cats like fish." On \
"Lemon" "You know how it tastes." on
Calendar
dialog --title "Christmas" --clear --calendar "Please choose a date..." 0 0 24 12 2025
File select
dialog --title "Please choose a file" "$@" --fselect "$HOME/" 14 48
For more examples, see “sample” directory in the source code found here.