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?


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.

  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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