Bash scripts are powerful but often limited to a bare text interface. Whiptail changes the game by allowing you to integrate lightweight graphical interfaces directly into your shell scripts. This utility, included by default on many Linux distributions, opens up a new range of possibilities for making your scripts more interactive and user-friendly. Whiptail: The ideal tool to boost your Bash scripts Originally developed for Linux environments,
Whiptail
is an improved clone of the dialog utility. Its unique feature? It allows you to create text-based dialog boxes that simulate a graphical interface, all without leaving the terminal. Quick Installation on All Distributions Before use, check for Whiptail with: whiptail -v If the tool isn’t installed, here’s how to add it depending on your distribution:Distribution Installation Command Package
Debian/Ubuntu
sudo apt install whiptail -y
<!– wp:code {"content":"whiptail -v« } –>whiptail
RHEL/CentOS
| sudo yum install newt -y | newt | Arch Linux |
|---|---|---|
| sudo pacman -S libnewt | libnewt |
https://www.youtube.com/watch?v=IKF6C8I8ZD8 |
| Mastering Whiptail Dialog Boxes | Whiptail's basic syntax follows this pattern: |
Available Dialog Box Types |
| 📌 | Message box |
: displays a simple message |
Yes/No box
: offers a binary choice
<!– wp:code {"content":"whiptail --option "Message" hauteur largeur [paramu00e8tres supplu00e9mentaires]« } –>⌨️
- Input box : allows text entry 🔒
- Password box : mask Input 📊
- Gauge : displays a progress bar 📋
- Menu : offers a single selection ☑️
- Checklist : allows multiple choices 🔘
- Radiolist : single selection with default choices Practical usage examples
- Creating an interactive menu: CHOICE=$(whiptail –menu « Choose an action: » 15 60 4 « 1 » « Save »
- « 2 » « Restore » « 3 » « Configure » « 4 » « Exit »
3>&1 1>&2 2>&3)
Dynamic progress bar:
<!– wp:code {"content":"CHOIX=$(whiptail --menu "Choisissez une action :" 15 60 4 n"1" "Sauvegarder" n"2" "Restaurer" n"3" "Configurer" n"4" "Quitter" n3>&1 1>&2 2>&3)« } –><!– wp:code {"content":"{ for i in {1..100}; do echo $i sleep 0.05 done } | whiptail --gauge "Operation in progress..." 10 50 0
{n for i in {1..100}; don echo $in sleep 0.05n donen} | whiptail --gauge "Opu00e9ration en cours..." 10 50 0« } –>Optimizing the user experience with Whiptail For truly professional scripts, Whiptail offers several customization options: Option Description Example --title
–title « Configuration »
–backtitle
| Adds background text | –backtitle « System Admin » | –defaultno |
|---|---|---|
Activates "No" by default |
–yesno –defaultno | --scrolltext |
Enables scrolling for long text |
–msgbox –scrolltext | Advanced Input/Output Management |
Redirection |
3>&1 1>&2 2>&3 | may seem complex but is essential for: |
📥 Correctly capturing user input |
📤 Managing stderr and stdout streams | 🔄 Enabling seamless integration into scripts |
Complete example with error handling:
NAME=$(whiptail –inputbox « Enter your name: » 10 50 3>&1 1>&2 2>&3) echo "Hello, $NAME" else
- echo « Operation canceled »
- fi
NOM=$(whiptail --inputbox "Entrez votre nom :" 10 50 3>&1 1>&2 2>&3)nnif [ $? -eq 0 ]; thenn echo "Bonjour, $NOM"nelsen echo "Opu00e9ration annulu00e9e"nfi« } –>