74

I want to make some graphical dialogs for my script but don't know how. I hear something about GTK-Server or something like that. If someone knows how to link Bash with tcl/tk I also be satisfied.

Please do not post something like "change to C++" because my project must be a script in Bash; there are no other options.

Any ideas?

EDIT: Thanks for answers but I don't want "graphics" as in colors in the console, but graphical windows which I can move, minimize etc. I will test xmessage, but I don't think that will be what I am searching for.

EDIT 2: I don't want make a simple dialog like yes/no, but some interface like progress bars and buttons, something like a game.

5
  • 3
    You definitely want zenity. Commented Aug 29, 2012 at 1:42
  • 1
    In the old days there was "dtksh" a ksh-93 implementation with access to Motif libraries. You could set shell function callbacks and make a full graphical application with a shell scripts. It featured dialog, button, textbox, radio-button,... Commented Nov 4, 2015 at 13:39
  • 1
    Have a look at tkbash. Commented Aug 12, 2017 at 17:12
  • the question is 8 years old. Commented Aug 14, 2017 at 18:15
  • unclosed dupe: stackoverflow.com/questions/8354482/dialog-from-bash-script Commented Nov 15, 2017 at 20:56

8 Answers 8

73

Before actually using GUI dialogues, consider using console prompts. Quite often you can get away with simple "y/n?" prompts, which in bash you achieve via the read command..

read -p "Do something? ";
if [ $REPLY == "y" ]; then
    echo yay;
fi

If console prompt's just won't cut it, Zenity is really easy to use, for example:

      zenity --error --text="Testing..."
      zenity --question --text="Continue?"

This only works on Linux/Gnome (or rather, it'll only be installed by default on such systems). The read method will work on pretty much any platform (including headless machines, or via SSH)

If you need anything more complex than what read or Zenity provides, "change to C++" is really the best method (although I'd recommend Python/Ruby over C++ for such shell-script-replacement tasks)

I want to do simple interface for some strange game, the progress bar for health or something is the example for what I want. Variable "HEALTH" is 34, so make progress bar filled in 34/100

As a command-line script, it'd use Python:

$ export HEALTH=34
$ python -c "import os; print '*' * int(os.environ.get('HEALTH', 0))"
**********************************

Or to normalise the values between 1 and 78 (so you don't get line-wrapping on a standard terminal size):

$ python -c "import os; print '*' * int((int(os.environ.get('HEALTH', 0)) / 100.0) * 78)"

Zenity also has a Progress Dialog,

#!/bin/sh
(
echo "10" ; sleep 1
echo "# Updating mail logs" ; sleep 1
echo "20" ; sleep 1
echo "# Resetting cron jobs" ; sleep 1
echo "50" ; sleep 1
echo "This line will just be ignored" ; sleep 1
echo "75" ; sleep 1
echo "# Rebooting system" ; sleep 1
echo "100" ; sleep 1
) |
zenity --progress \
  --title="Update System Logs" \
  --text="Scanning mail logs..." \
  --percentage=0

if [ "$?" = -1 ] ; then
        zenity --error \
          --text="Update canceled."
fi

As I said before, if Zenity cannot do what you need, look into writing your game-thing as a "proper" script in Python/Ruby/Perl/C++/etc as it sounds like you're pushing the bounds of what a shell-script can do..

Sign up to request clarification or add additional context in comments.

3 Comments

I don't want the yes/no, but I want to do simple interface for some strange game, the progress bar for health or something is the example for what I want. Variable "HEALTH" is 34, so make progress bar filled in 34/100. That I want to do.
The Progress Dialog link above is broken :(
So easy to use: Forbidden You don't have permission to access /users/zenity/stable/ on this server. Apache/2.2.15 (Red Hat) Server at help.gnome.org Port 80
25

If you want to write a graphical UI in bash, zenity is the way to go. This is what you can do with it:

Application Options:
  --calendar                                     Display calendar dialog
  --entry                                        Display text entry dialog
  --error                                        Display error dialog
  --info                                         Display info dialog
  --file-selection                               Display file selection dialog
  --list                                         Display list dialog
  --notification                                 Display notification
  --progress                                     Display progress indication dialog
  --question                                     Display question dialog
  --warning                                      Display warning dialog
  --scale                                        Display scale dialog
  --text-info                                    Display text information dialog

Combining these widgets you can create pretty usable GUIs. Of course, it's not as flexible as a toolkit integrated into a programming language, but in some cases it's really useful.

1 Comment

Except the manual is useless: help.gnome.org/users/zenity
18

there is a command called dialog which uses the ncurses library. "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)"

see http://pwet.fr/man/linux/commandes/dialog

2 Comments

Funny enough, I was just installing a version of the utility when I cam across your answer. Dialog works quite well I think.
In addition to the dialog utility, whiptail should also be mentioned. They even maintain some level of compatibility between each other.
15

Well, if you can use Tcl/Tk in your environment, you probably should write a TCL script and use that. You might also look at wish.

2 Comments

but that is not bash
It might not be bash, but there's a limit beyond which bash gets really awkward and doing GUIs of any complexity is well beyond that.
11

You could use dialog that is ncurses based or whiptail that is slang based.

I think both have GTK or Tcl/Tk bindings.

Comments

7

You can gtk-server for this. Gtk-server is a program that runs in background and provides text-based interface to allow other programs (including bash scripts) to control it. It has examples for Bash (http://www.gtk-server.org/demo-ipc.bash.txt, http://www.gtk-server.org/demo-fifo.bash.txt)

1 Comment

Sounds interesting. Unfortunately, there don't seem to be .deb packages for it. Will try to compile it anyway.
7

Please, take a look at my library: http://sites.google.com/site/easybashgui

It is intended to handle, with the same commands set, indifferently all four big tools "kdialog", "Xdialog", "cdialog" and "zenity", depending if X is running or not, if D.E. is KDE or Gnome or other. There are 15 different functions ( among them there are two called "progress" and "adjust" )...

Bye :-)

2 Comments

cool, so it is an adapter. Are the supported tools required or you lib will use whatever it find, compatibly with the environment detected?
This one is pretty cool actually. The bash programmer need not worry about which platform the user will be using KDE or gnome; it will automatically detect the environment and show the UI accordingly using the available libraries.
5

If you have Qt/KDE installed, you can use kdialog, which pops up a Qt dialog window. You can easily specify to display a Yes/No dialog, OK/Cancel, simple text input, password input etc. You then have access to the return values from these dialogs at the shell.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.