2

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!!

  • You want to set DISPAY to your current display value? – P.-H. Lin Feb 5 '15 at 9:41
  • Yes, but need to assign in a shell script. – Chetan G Feb 5 '15 at 9:42
  • Did you realize that you have a typo in your sh script? It's missing an S: DISPLAY vs. (your) DIPLAY. ;) – kaiser Jan 13 '16 at 4:47
  • This solved for me, because I was look for a way to store DISPLAY value to a variable: stackoverflow.com/questions/1057343/… – Redbob Jun 5 '17 at 3:07
3

I can't comment as I don't have 50 points yet. Just want to comment on the answer by muru: awk needs to print the third column for the DISPLAY number (at least in 16.04), since the second column "$2" prints the tty number.

so instead of:

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

it should be:

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

I use:

DISPLAY_M=$(echo "export DISPLAY"$(w -h $USER | awk '$3 ~ /:[0-9.]*/{print $3}' | sed 's/\:/=\:/')

When called it prints the full export DISPLAY=:0. The sed part is to add the = to the echo.

Thanks muru.

1

The command should be:

#!/bin/bash    
export DISPLAY=$(echo $DISPLAY)

Here you need to use the command substitution functionality i.e. $(command) of bash.

EDIT:

Now after seeing your full script i am getting the whole picture.

First create a file in your home directory for example /home/abc/display_check having the following line:

echo $DISPLAY

Now in your original script add a line source /home/abc/display_check i.e. your original script can take the following form:

#!/bin/bash 
export DISPLAY=$(source /home/abc/display_check)
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 have tried with above code but still unable to make the output. – Chetan G Feb 5 '15 at 10:07
  • change the shell to bash. Check my edits. – heemayl Feb 5 '15 at 10:09
  • Nope....#!/bin/bash will ensure that it is being interpreted by bash. – heemayl Feb 5 '15 at 10:23
  • Not Working with the above change. – Chetan G Feb 5 '15 at 10:33
  • @ChetanG: can you show me an example of what you are doing.. – heemayl Feb 5 '15 at 10:35
1

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.
  • @muru When tried w -h $USER | awk '$2 ~ /:[0-9.]*/{print $2}' prints :0 – Chetan G Feb 5 '15 at 12:21
  • 1
    @ChetanG that's the idea. The display for your logged in user is shown. If it is supposed to be something else, how is it derived? – muru Feb 5 '15 at 13:19

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.