How-To Geek

How to Make Simple Graphical Shell Scripts with Zenity on Linux

Zenity adds graphical interfaces to shell scripts with a single command. Shell scripts are a great way to automate repetitive tasks, but they’re normally confined to the terminal — Zenity brings them out of the terminal and onto your desktop.

We’ve given an introduction to shell scripting in the past. You don’t have to be a programmer to get started with shell scripts — they require little more than knowledge of Linux terminal commands.

Getting Zenity

Zenity comes with Ubuntu by default. If you use an Ubuntu derivative, Such as Kubuntu, you may have to install it manually with the following command:

sudo apt-get install zenity

Zenity is a part of GNOME, so it should already be included on Linux distributions that use the GNOME desktop. Check your package manager for the zenity package if you don’t have it.

Using Zenity

You can play around with Zenity from the terminal. Let’s say you want to create an error window when a problem occurs with your shell script. Here’s an example command you could use:

zenity –error –title=”An Error Occurred” –text=”A problem occurred while running the shell script.”

Run the command and you’ll see a window with the message.

Put this single command into your shell script at the correct place and you’ll have a graphical error message. You could also use variables to include more information about the error.

Let’s say you want to ask a yes or no question. You could use a command like this one:

zenity –question –title=”Query” –text=”Would you like to run the script?”

You can catch the yes or no response in your shell script and perform different commands based on which button the user clicks.

There’s also a text entry dialog:

zenity –entry –title=”Favorite Website” –text=”What is your favorite website?”

Catch the user’s input in a shell script and you could store it as a variable.

There’s also a file picker, calendar, and other types of dialogs. For a full list of dialog types and their options, consult Zenity’s manual page.

An Example Script

Let’s try using Zenity to create a simple graphical shell script. With just three commands, we can create a graphical timer program:

# This script asks the user for a time, waits the specified amount
# of time, and shows an alert dialog.

TIME=$(zenity –entry –title=”Timer” –text=”Enter a duration for the timer.\n\n Use 5s for 5 seconds, 10m for 10 minutes, or 2h for 2 hours.”)

sleep $TIME

zenity –info –title=”Timer Complete” –text=”The timer is over.\n\n It has been $TIME.”

We’re using some extra tricks here. We get the value of the TIME variable from the first zenity command and feed it to the sleep command. We’re also using /n to create new lines of text in the zenity dialogs.

After saving the shell script and running the chmod +x command on it to give it executable permissions, we can launch it.

Enter a duration and the script will use the standard sleep command to count down in the background. When the sleep command’s timer finishes, the script will display the zenity info message.

You could create a desktop or panel shortcut for this script and run it without even touching the terminal.

This is just scratching the surface of what you could do with zenity; you could use it to make much more complicated programs. If you’re looking for more information on shell scripting, check out our guide to using for loops in shell scripts.

Chris Hoffman is a technology writer and all-around computer geek. He's as at home using the Linux terminal as he is digging into the Windows registry. Connect with him on Google+.

  • Published 03/9/12

Comments (10)

  1. El Achèche ANIS

    Shit!!! That’s so weird!!! I was just googling about that (GUI for Shell) and learning some articles about Zenity!! Next time I’ll check my RSS feeds list before googling X)

  2. TheFu

    Golf clap!

    Shell scripting changes computing from
    a) you doing things on a computer
    b) the computer doing things for you.

    Bash has many cool features. My nearly 1-stop how-do-I X in bash is the Advanced Bash-Scripting Guide – It isn’t about GUI/Zenity, but we all need to know this stuff to make computers of all sorts work FOR us.

    A few really cool settings/options to make your bash scripts more bullet proof are:
    * errexit
    * pipefail
    * trap
    * nounset
    * errtrace
    and that you can declare variables to be read-only or integers. To learn more, here’s a copy of Michael Potter’s most excellent presentation:

    I saw Michael give this presentation last month and I’m certain there were subtle things that I missed … even with 15+ yrs of shell scripting practice.

  3. Eric

    No kdialog?

  4. Thomas ONeil

    Yaaaaaahhhhhh!! Been looking for something simple, but effective like this to integrated a file chooser into scripts. Wow. What a cool utility! THANK YOU for the article!!

  5. Grant

    This also works on Mac with the Fink Project.

  6. Chris Hoffman


    Yup, KDE users can use kdialog. I’m sure there are other options, too!

  7. kenedy123

    Thanks for giving the more information about the How to Make Simple Graphical Shell Scripts with Zenity on Linux and it is helpful.

  8. Saint DanBert

    For those who want similar features without needing a supporting desktop goo-wee (GUI).
    Let us remember the ‘dialog’ package. According to its man-page:

    “Dialog is a program that will let you to present a variety of questions or display messages using dialog boxes from a shell script. These types of dialog boxes are implemented (though not all are necessarily compiled into dialog):=== calendar, checklist, dselect, editbox, form, fselect, gauge, infobox, inputbox, inputmenu, menu, mixedform, mixedgauge, msgbox (message), passwordbox, passwordform, pause, progressbox, radiolist, tailbox, tailboxbg, textbox, timebox, and yesno (yes/no)”

    ~~~ 8d;-Dan

  9. Saint DanBert

    Actually, ‘dialog’ relies on the “graphics terminal” features of the console or xterm where it is running.
    For x86-family workstation, that is VT100-family (ANSI v3.64) escape sequences.

    There used to be a ‘curses’ or ‘ncurses’ (not graphic) edition of dialog, but I cannot find it any longer.

    Sorry for any confusion,
    ~~~ 8d;-/ Dan

  10. Chris Davies

    The dialog package (certainly the one provided with Debian/stable) runs fine in a terminal window. By default it uses “drawing” characters to present its box frames, but the –ascii-lines option forces it to revert to basic 7bit ASCII characters (+, -, |). Both methods appear to use the teminfo database to derive the appropriate positioning and drawing characters, which is a correct solution for something writing to a terminal.

    A quick check of the libraries linked into dialog (ldd `type -p dialog`) also shows that ncurses is included, which is unsurprising given that it provides a much higher level interface than the raw terminfo library.

    Hope this helps

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers


Go check your email!