Take the 2-minute tour ×
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.

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

1 Answer 1

up vote 8 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
else
    # run term program
fi

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

 
discard

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.