How set environment variable based on whether a GUI is available

environment-variablesx11

How can I set up my .bashrc such that the value of an environment variable changes based on whether or not the current session has access to the gui / windowing system?

For example, let's say I have an environment variable that controls which diff program my VCS will use.

When I'm sitting at the machine, I'd like to use a GUI program. However, when I connect remotely via SSH, I'd like to have it automatically use a text-based terminal diff viewer instead.

How can I check whether or not the windowing system is available?

Best Answer

You can check the $DISPLAY variable to see whether you're on an x display - if it's non-empty, you have a display:

if [ -n "$DISPLAY" ]; then
    # run GUI program
else
    # run term program
fi

A quick test showed this even works for X-tunneling.

Related Question