Ubuntu – Export $DISPLAY value in Shell script

bashdisplayscripts

I am trying to Print the value of $DISPLAY under my Shell script.

I have Following shell script which Exports the DISPLAY:

#!/bin/sh
export DIPLAY=:10.0

Here, each time I need to change the Value manually to accomplish the task.

Can we make the DISPLAY value dynamic using command:

echo $DISPLAY  'Prints the current Display Value :10.0

I have tried with the below code but it won't assigns the DISPLAY value to the variable:

#!/bin/sh
export DIPLAY=echo $DISPLAY

Below is the complete Code:

#!/bin/sh 
export DISPLAY=:10.0 
export ANT_HOME=/home/abc/Desktop/Eclipse/plugins/org.apache.ant_1.9.2.v201404171502 
cd /home/abc/Desktop/auto/Automation/xyz 
ant usage clean compile build run makexsltreports sendemail 

I am running this shell script every hour using crontab job

Please suggest!!

Best Answer

echo $DISPLAY won't work. If it did, you wouldn't need to set or export DISPLAY in the first place. You'll need to find out what the appropriate value is using some other way.

If you want to find out what DISPLAY your user is currently running, try:

w -h $USER | awk '$2 ~ /:[0-9.]*/{print $2}'

Then you can do:

export DISPLAY=$(w -h $USER | awk '$2 ~ /:[0-9.]*/{print $2}')
  • w lists the currently logged-in users and where they are logged in from, with DISPLAY being used for users logged in by GUI.
  • With awk, we match the second field, the location, to something that looks like a DISPLAY and print it out.
Related Question