How to check if export DISPLAY is set or not?

Hi All,

I use "export DISPLAY=same_host:0.0" to set my export DISPLAY and it is working fine for me..

Problem here is I have developed a script for which i should run export DISPLAY prior to running my script....

so my script should check whether export DISPLAY is set or not.. if set then it should continue else it should give error.. Please Help me

[ -z "$DISPLAY" ] && echo Display not set && exit 1

Hey Scottn thanks for your reply.. but i'm not clear how to use this.. I'm not that good at Unix as I'm a mechanical engineer.. Should i use this with if statement or what? Please advice

If you want to check if the variable DISPLAY is exported from your environment and not only defined, you can do :

export | grep -q '^DISPLAY=' && echo Ok || echo Not Exported
$ unset DISPLAY
$ export | grep -q '^DISPLAY=' && echo Ok || echo Not Exported
Not Exported
$ DISPLAY=value
$ export | grep -q '^DISPLAY=' && echo Ok || echo Not Exported
Not Exported
$ export DISPLAY=value                                        
$ export | grep -q '^DISPLAY=' && echo Ok || echo Not Exported
Ok
$

Jean-Pierre.

OH tried this and working fine.. Thank you very much Scottn...

Hi.

No, you can use it as-is. Place it near the top of your script.

If DISPLAY is not exported, your script won't see it and this will print a message and exit.

It's the same as

if [ -z "$DISPLAY" ]; then
  echo Display not set
  exit 1
fi

If you need to check for a specific value in DISPLAY, you can try this:

[ "$DISPLAY" != "same_host:0.0"  ] && echo Display not set && exit 1

thanks to aigles also...his code is also working fine... Thanks a lot to you both...