DEV Community

Cover image for Building GUI for your shell script using zenity
Rafi
Rafi

Posted on

Building GUI for your shell script using zenity

Sometimes the shell scripts that you write asks questions to the user so that they could change the some default value. We usually do this as text input. But for values like date, file path etc.. it would be nice if you show a calendar or a file picker.

There is a really nice command called zenity. Which allows you to build simple GUI for your shell script. Making your shell script more user friendly.

Let's say you want user to select a date you can do

$ zenity --calendar
Enter fullscreen mode Exit fullscreen mode

and it will return you selected date as string

Alt Text

and for a file selection you can simply do

$ zenity --file-selection
Enter fullscreen mode Exit fullscreen mode

When your shell scripts finishes you can notify the user using

$ zenity --notification --text "script completed"
Enter fullscreen mode Exit fullscreen mode

zenity can do much more and it is has been out there for a while and it is available for most Linux distributions and even for other OS like Mac and Windows (but you may need to install them separately)

Edit: You can look for more details about all the options that zenity provides by runinng

$ man zenity
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
jrop profile image
Jonathan Apodaca

I use a similar (TUI-based) one called whiptail. It's nice for bootstrapping a new system :D

Collapse
 
rafi993 profile image
Rafi • Edited

It is really cool. I once came across dialog command which is similar to this. I wonder what other similar commands are out there for building UI.

Collapse
 
manishfoodtechs profile image
manish srivastava

Can you write more detailed usage. Nice for sharing

Collapse
 
rafi993 profile image
Rafi

since man pages of zenity

$ man zenity

has all other options described in detail I thought I would leave it rather than replicating it. But it would be nice to demonstrate zenity with some specific use-case in mind.