182

I'm writing a few little bash scripts under Ubuntu linux. I want to be able to run them from the GUI without needing a terminal window to enter any input or view any output.

So far the only input required is a password for sudo - and gksudo handles that fine. But I haven't found an easy way to show a message box yet. Is there some kind of 'gkmessage' command available? I'd prefer something present in a default Ubuntu install, but I don't mind installing a new package if necessary.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272

14 Answers14

185

In many Linux distros the notify-send command will throw one of those nice perishable notifications in the top right corner. Like so:

notify-send "My name is bash and I rock da house"

B.e.a.utiful!

ecoe
  • 4,994
  • 7
  • 54
  • 72
chmac
  • 11,757
  • 3
  • 32
  • 36
  • 2
    This works with Fedora as well. I'm pretty certain that any Linux distro can do this. – kmatheny Sep 10 '13 at 17:26
  • 5
    Doesn't work on Raspbian GNU/Linux 7. Is there a package that needs to be installed ? – carl verbiest Jun 06 '15 at 06:26
  • 2
    in the debian repos: apt-cache search notify-osd @carlverbiest – santa Jul 15 '15 at 13:57
  • 3
    in Ubuntu 14.04 at least, if call notify-send with timeout 0 like so "notify-send -t 0 'hi there!'" you will get a popup dialog which does not expire. – vancan1ty May 08 '16 at 03:07
  • 1
    In Debian Jessie the `libnotify-bin` package contains the `notify-send` binary. Gnome3 does not appear to require `notify-osd` but I guess other desktop environments may require that in addition to `libnotify-bin` – Jasen Oct 16 '16 at 20:54
  • This is also available by default on Slackware 14.2 – DaveGauer Mar 27 '17 at 22:19
  • Works beautifully with LXQt, where none of the other alternatives is installed by default. – Murphy Oct 31 '19 at 23:01
  • `GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Notifications was not provided by any .service files` – alper Jan 21 '23 at 14:46
164

I believe Zenity will do what you want. It's specifically designed for displaying GTK dialogs from the command line, and it's available as an Ubuntu package.

Derek Park
  • 45,824
  • 15
  • 58
  • 76
  • 7
    `zenity --help` is not so helpful. To make this useful, you need to ensure that you set `--text=My text here` to make it display something... – Stephen Oct 26 '11 at 04:03
  • 14
    E.g. `zenity --info --text="Calculation complete"` – Craig McQueen Dec 23 '13 at 01:32
  • 5
    @Stephen, for some odd reason the correct command is `zenity --help-all`. – GKFX Sep 09 '14 at 19:26
  • 6
    I don't like programs that need to open the display in order to print the help message... – thoni56 Nov 11 '16 at 12:09
  • I use this method to display warnings or errors within my `.xinitrc` while starting my `xorg` server. Otherwise the errors would be transparent. I also prefer this `zenity`over `kdialog` since KDE didn't start yet and GTK libs are more lightweight. – cmevoli Dec 16 '16 at 12:35
  • @thoni56 not a real nuisance since all uses of this program need a X display, yet true: `unset DISPLAY ; LC_ALL=C zenity --help-all Unable to init server: Could not connect: Connection refused (zenity:32545): Gtk-WARNING **: 09:24:17.177: cannot open display: ` – Stéphane Gourichon Feb 06 '19 at 08:25
  • [zenity docs](https://help.gnome.org/users/zenity/stable/) – Pedro Lobito Jul 20 '20 at 00:14
122

Everyone mentions zenity, there seem to be many others. A mixed up but interesting list is at http://alternativeto.net/software/zenity/

zenity:

First, an example of zenity featuring text formatting markup, window title, button label.

zenity \
--info \
--text="<span size=\"xx-large\">Time is $(date +%Hh%M).</span>\n\nGet your <b>coffee</b>." \
--title="Coffee time" \
--ok-label="Sip"

Screenshot

gxmessage:

gxmessage "my text"

Screenshot

xmessage:

xmessage is very old so it is stable and probably available in all distributions that use X (since it's distributed with X). It is customizable through X resources, for those that have been using Linux or Unix for long enough to know what it means (.Xdefaults, anyone ?).

xmessage -buttons Ok:0,"Not sure":1,Cancel:2 -default Ok -nearmouse "Is xmessage enough for the job ?" -timeout 10

Screenshot

kdialog (KDE tool):

kdialog --error "Some error occurred"

Screenshot

YAD (Yet Another Dialog):

Yad is included in newer Ubuntu versions. There is also this PPA: YAD: Zenity On Steroids [Display Graphical Dialogs From Shell Scripts] ~ Web Upd8: Ubuntu / Linux blog. Does not seem to auto-size dialogs.

echo My text | yad \
--text-info \
--width=400 \
--height=200

An bigger example

yad \
--title="Desktop entry editor" \
--text="Simple desktop entry editor" \
--form \
--field="Type:CB" \
--field="Name" \
--field="Generic name" \
--field="Comment" \
--field="Command:FL" \
--field="Icon" \
--field="In terminal:CHK" \
--field="Startup notify:CHK" "Application" "Name" "Generic name" "This is the comment" "/usr/bin/yad" "yad" FALSE TRUE \
--button="WebUpd8:2" \
--button="gtk-ok:0" \
--button="gtk-cancel:1"

Screenshot

Others not in Ubuntu standard repositories:

  • shellgui
  • xdialog
  • gtkdialog

Off-topic (for terminal):

whiptail --msgbox "my text" 10 20
dialog --msgbox "my text" 10 20

Feel free to edit.

Flimm
  • 136,138
  • 45
  • 251
  • 267
Stéphane Gourichon
  • 6,493
  • 4
  • 37
  • 48
  • 2
    Note, [whiptail's `--infobox` has a long standing display bug with xterm (gnome-terminal)](http://stackoverflow.com/a/15192893/1695680) – ThorSummoner Jul 24 '15 at 18:02
  • @ThorSummoner thanks for the tip. For the interested reader, the difference is that `whiptail --infobox` (which does not work properly on terminals that support alternate screen) returns without waiting for user input, while `whiptail --msgbox` (which works) waits for user confirmation before exiting. – Stéphane Gourichon Jan 06 '16 at 08:43
  • I recall being unsatisfied with msgbox, for my purposes at the time, I think I needed the output to be viewable in a log or something like that; – ThorSummoner Jan 06 '16 at 19:12
  • A lot of options! Pretty nice examples – artu-hnrq Feb 24 '20 at 07:27
51

The zenity application appears to be what you are looking for.

To take input from zenity, you can specify a variable and have the output of zenity --entry saved to it. It looks something like this:

my_variable=$(zenity --entry)

If you look at the value in my_variable now, it will be whatever was typed in the zenity pop up entry dialog.

If you want to give some sort of prompt as to what the user (or you) should enter in the dialog, add the --text switch with the label that you want. It looks something like this:

my_variable=$(zenity --entry --text="What's my variable:")

Zenity has lot of other nice options that are for specific tasks, so you might want to check those out as well with zenity --help. One example is the --calendar option that let's you select a date from a graphical calendar.

my_date=$(zenity --calendar)

Which gives a nicely formatted date based on what the user clicked on:

echo ${my_date}

gives:

08/05/2009

There are also options for slider selectors, errors, lists and so on.

Hope this helps.

Jim
  • 1,217
  • 10
  • 16
22

I found the xmessage command, which is sort of good enough.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
11

alert and notify-send seem to be the same thing. I use notify-send for non-input messages as it doesn't steal focus and I cannot find a way to stop zenity etc. from doing this.

e.g.

# This will display message and then disappear after a delay:
notify-send "job complete"

# This will display message and stay on-screen until clicked:
notify-send -u critical "job complete"

Screenshot

Flimm
  • 136,138
  • 45
  • 251
  • 267
11

if nothing else is present. you can launch an xterm and echo in it, like this:

 xterm -e bash -c 'echo "this is the message";echo;echo -n "press enter to continue "; stty sane -echo;answer=$( while ! head -c 1;do true ;done);'
user6795571
  • 139
  • 1
  • 7
9

Here's a little Tcl script that will do what you want. The Wish interpreter should be installed by default on Ubuntu.

#!/usr/bin/wish
pack [label .msg -text [lindex $argv 0]]
pack [entry .ent]
bind .ent <KeyPress-Return> { puts [.ent get]; destroy . }
focus .ent

Call it like this:

myanswer=`gui-prompt "type your answer and press enter"`
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
7

There is also dialog and the KDE version kdialog. dialog is used by slackware, so it might not be immediately available on other distributions.

Michael Hoffmann
  • 2,411
  • 2
  • 24
  • 42
Steve Baker
  • 4,323
  • 1
  • 20
  • 15
  • `dialog` is available in the Ubuntu repos, and displays a dialog with the terminal (TUI). It's not a GUI. – Flimm Feb 10 '23 at 16:16
4

How about Ubuntu's alert. It can be used after any operation to alert it finished and even show red cross icon if operaton was finnished with errors

ls -la; alert
mulya
  • 1,273
  • 13
  • 26
3

You can use shellmarks to display a GUI dialog prior to your shell script running, that will allow the user to enter data that will be placed in the environment.

#!/bin/bash
echo "Hello ${name}"
exit 0
---
[name]
  type="text"
  label="Please enter your name"
  required=true

Running script:

shellmarks hello.sh

Shellmarks dialog

If you enter "Steve" in the box and press run, the output will be

Hello Steve

Disclosure: I'm the author of Shellmarks

steve hannah
  • 4,586
  • 11
  • 18
3

Zenity is really the exact tool that I think that you are looking for.

or

zenity --help
Lnux
  • 319
  • 1
  • 6
  • 15
0

I'm liking what I'm seeing with script-dialog. It ticks all my boxes, plus some:

  • pop up GUI boxes, but has text-mode fallback
  • support for various sudo variants (gksudo, kde-sudo, ...)
  • can re-launch itself in terminal window

Indeed it's a wrapper for kdialog, zenity, dialog, whiptail and a custom fall-back.

Draw-back is that it doesn't have a CLI, but instead is meant to be sources into a bash script.

Bendlas
  • 787
  • 3
  • 12
0

Kdialog and dialog are both good, but I'd recommend Zenity. Quick, easy, and much better looking the xmessage or dialog.

Jarek
  • 1,320
  • 3
  • 11
  • 19