Improve Bash Ux with Whiptail

Debian-based distributions include a package called whiptail that can improve user experience significantly.

Instead of reading raw text in the terminal, the user gets a clean UI:

It’s particularly helpful to populate text inputs, passwords, checklists, selects or to achieve “yes-no interactions.”

Getting started

The wiki is quite straightforward, so if you need to build a quick bash script, whiptail seems a great additional layer.

The code is also more readable to me:

if (whiptail --title "Example Dialog" --yesno "This is an example of a yes/no box." 8 78); then
    echo "User selected Yes, exit status was $?."
else
    echo "User selected No, exit status was $?."
fi

You can use whiptail directly as an if/then statement.

You can even build complex forms in Bash to get inputs from users and also display progress bars for your script:

#!/bin/bash
{
    for ((i = 0 ; i <= 100 ; i+=5)); do
        sleep 0.1
        echo $i
    done
} | whiptail --gauge "Please wait while we are sleeping..." 6 50 0

What is the purpose of 8 78 or 6 50 0 in the code?

If you wonder what are these numbers, it’s meant to set the dimensions of the dialog (e.g., width, height, etc).

For example, 8 78 refer to the columns. The idea is to select a size that won’t cover the whole screen, as the user might struggle to find the buttons to interact with your forms if you do so.

What are 3>&1, 1>&2 and 2>&3?

Whiptail sends the input to stderr instead of the classic stdout. That’s why you often need to reverse the redirection:

COLOR=$(whiptail --inputbox "What is your favorite Color?" 8 39 Blue --title "Example Dialog" 3>&1 1>&2 2>&3)

Wrap up

Whiptail comes by default in most Debian-based distributions and you can install it in other Linux flavors.

It goes way beyond the classic echo and read and provides a decent UI to enhance users’ interactions with the terminal.