How to detect availability of GUI in Bash/Shell?

how to create gui in shell script
zenity gui
linux server health check checklist
create gui for terminal commands
linux server health check shell script
shell script for linux server monitoring
how to check running process in linux
linux server health check commands

I'm writing a CLI in NodeJS. As I can easily run bash/shell commands using the child_process, I'd like to know the easiest most cross platform way to detect GUI availability in bash/shell.


On macOS, there's not a clearly appropriate way to check this from a shell, as such. There's a programmatic way, and we can use an interpreted language to take advantage of that.

Here's a little script that outputs one of three states, Mac GUI, Mac non-GUI, or X11:

if [ `uname` = "Darwin" ]
    if which swift >/dev/null && swift <(cat <<"EOF"
import Security
var attrs = SessionAttributeBits(rawValue:0)
let result = SessionGetInfo(callerSecuritySession, nil, &attrs)
exit((result == 0 && attrs.contains(.sessionHasGraphicAccess)) ? 0 : 1)
        echo "Mac GUI session"
    elif [ -n "$DISPLAY" ]
        echo "Mac X11 GUI session"
        echo "Mac non-GUI session"
elif [ -n "$DISPLAY" ]
    echo "X11 GUI session"

Macs can have an X server installed, in which case DISPLAY is defined. However, I don't know if your Electron app will work properly in that configuration. So, I detected it separately.

how to detect availability of GUI, For Linux: I have a text mode installation (bash) script and a graphical mode setup program (a Free Pascal binary). I'd like to run the graphical setup when a GUI� The if command checks the exit code of the following command (the ping). If the exit code is zero (which means that the command exited successfully) the then block will be executed. If it return a non-zero exit code, then the else block will be executed.

Check if DISPLAY is set in the environment. If it is, you have an Xserver running. If not, you don't. Pretty sure that wayland sets it too.

How to Add a GUI to Linux Shell Scripts, It's part of GNOME. If you use KDE, you might want to check out kdialog instead, although zenity does run on any desktop environment. The� Type this command: ls /usr/bin/*session Based on what you see on the screen you can find out what GUI you are using right now. For example you are: in GNOME if it returns /usr/bin/gnome-session (and more); in MATE if it returns /usr/bin/mate-session (and more); in LXDE if it returns /usr/bin/lxsession (and more);

Here is a working example:

if [ x$DISPLAY != x ] ; then
  echo "GUI Enabled"

  echo "GUI Disabled"


All this does is checks the $DISPLAY variable.

16 Linux server monitoring commands you should know | HPE, That's because Linux GUIs take up system resources that could be better used elsewhere. turn off the GUI and use these tools from the Linux command shell. For day-to-day server monitoring, I find nmon to be the single most useful Corporate Responsibility Global Diversity & Inclusion Supply Chain� The system I'm working on is quite minimal, all I can see are xterm windows, from where I can open applications (in separate windows). I can open new xterm window by right click in a free area on the desktop.

Try this : echo $XDG_CURRENT_DESKTOP Or this : echo $DESKTOP_SESSION

Bash check if process is running or not on Linux / Unix, How do I determine whether a process is running or not in a shell script running on an Ubuntu server? Introduction: You can quickly determine if a� You can use GUI windows, sliders, radio buttons, progress bars, and more in your Bash scripts. Learn how to use the zenity toolkit and give your Bash scripts a facelift. We’ll show you how. Bash scripting is a powerful programming language and, because it’s built into the Bash shell, it’s readily available to everyone. It’s an easy

Linux Check Memory Usage Using the CLI and GUI, -s : update every [delay] seconds. -c : update [count] times. vmstat command. Type the vmstat command at shell prompt: $ vmstat echo "$SHELL" – Display the shell for the current user but not necessarily the shell that is running at the movement. echo $0 – Another reliable and simple method to get the current shell interpreter name on Linux or Unix-like systems. cat /etc/shells – List pathnames of valid login shells currently installed.

9 commands to check if connected to internet with shell script , Validate internet connectivity with examples using shell bash script in There are various tools which can be used to check port availability� The following command will tell if a text file called /etc/hosts exists or not using bash conditional execution: [ -f / etc / hosts ] && echo "Found" || echo "Not found" [ -f /etc/hosts ] && echo "Found" || echo "Not found"

Linux commands to display your hardware information, The easiest way is to do that is with one of the standard Linux GUI programs: Whether you employ the Linux shell or another programming Many line commands for detecting hardware must be issued under root authority. As a result, many shell-script writers probably wish there were something like that for shell scripts. Well, there is a special way to code a GUI for scripts in the script itself. A special program called "Zenity" can be used in scripts to make GTK+-based windows.

  • If you're writing a CLI, why do you need GUI availability?
  • Because I want to launch an alternative electron app if GUI is available, it's fency :D
  • Probably checking for existence of the environment variable DISPLAY is the best bet. Not sure it's 100% reliable check on MacOS, though.
  • Indeed, the DISPLAY variable does not exist on OSX :/
  • Just launch your app. If there is a GUI, it will run. If not, it will fail in a predictable way. If you detect that predictable way, move into CLI operation - otherwise fail in error?
  • Why swift <(cat <<"EOF" instead of swift - <<"EOF"?
  • @CharlesDuffy If it receives its input from stdin rather than a filename argument, it behaves as though it's interactive and exits with 0 status regardless of what's passed to exit(). Also, it produces annoying extraneous output.
  • I wonder if we have different releases. swift - <<<'import Security; exit(3)'; echo $? emits no output at all (prior to the echo) and shows exit status 3 for me -- that's with Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)
  • ...or perhaps the behavior you mention above is without the explicit - specifying stdin?
  • Yeah, the explicit - seems to make the difference. For the record, I'm currently testing on Sierra with Xcode 8, so: Apple Swift version 3.1 (swiftlang-802.0.51 clang-802.0.41).
  • He tagged it as MacOS as well. Not sure DISPLAY will be set unless Xquartz is installed.
  • I missed that. I'm not sure, either.