Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 9 down vote accepted

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
    # run term program

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

share|improve this answer
Thanks! I knew I had seen a way to do this before, just couldn't find it anywhere. – TM. Feb 21 '12 at 22:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.