How do I find out my screen resolution from a shell script?

49

13

How do I find out my screen resolution from a shell script?

dave4420

Posted 2010-10-06T14:33:05.710

Reputation: 1 328

Answers

56

xdpyinfo | grep dimensions | sed -r 's/^[^0-9]*([0-9]+x[0-9]+).*$/\1/'

Command xdpyinfo displays various information about your X server. It writes a lot of things to the standard output but we only need the line starting with the word dimensions, thus we use grep. Finally we use sed to clean the result.

user31752

Posted 2010-10-06T14:33:05.710

Reputation:

If you need x and y axis dimension separately, you can do the following. First, put the result of the above command into a variable using $( ) syntax (i.e. DIMENSIONS=$(xdpyinfo ...). Then use sed again to get the two: WIDTH=$(echo $DIMENSIONS | sed -r 's/x.*//') and HEIGHT=$(echo $DIMENSIONS | sed -r 's/.*x//'). – mneri – 2017-02-15T16:57:56.887

xdpyinfo prints an error message if it cannot access information, so error redirection to /dev/null. For this reason you may want to add an error redirection: xdpyinfo 2> /dev/null. So, the full piple looks like this: xdpyinfo 2> /dev/null | grep dimensions | sed -r 's/^[^0-9]*([0-9]+x[0-9]+).*$/\1/'. This will make your script more solid. – mneri – 2017-02-15T19:11:57.490

xdpyinfo | grep dimensions: | awk '{print $2}' seems to be simpler and more readable. – x-yuri – 2019-12-04T08:15:46.717

29

xdpyinfo | grep dimensions will give you the total resolution, if you have multiple monitors it will be the sum of all of them. xrandr --current will give you the resolution for each monitor.

I use this snippet to find the maximum possible resolution for rDesktop without going to full screen:

Xaxis=$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f1)

Yaxis=$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f2)

Output:

Xaxis = 1280
Yaxis = 1024

Minus windows decoration (more or less):

MaxRes=$(($Xaxis-5))"x"$(($Yaxis-25))

Output:

MaxRes = 1275x999

Which is the max resolution for rDesktop without going full screen.

End command:

rdesktop -u $User -P -z -5 -g $MaxRes $Host &

It works fine so far but I haven't tested thoroughly though.

Another example is for screencast with avconv:

avconv -f x11grab -r 15 -s `xrandr --current | grep  '*' | uniq | awk '{print $1}'` -i :0.0 -c:v libx264 ./output.mp4

Eliezer E. Vargas

Posted 2010-10-06T14:33:05.710

Reputation: 291

it says> xdpyinfo: Unable to open display "". – To Kra – 2015-12-14T09:27:03.737

How do you find out the available modes to change to? – CMCDragonkai – 2016-02-08T06:32:45.483

If you don't need to subtract for window decoration (etc), you can do this in a one-liner rdesktop [other_args] -g $(xrandr --current | grep '*' | uniq | awk '{print $1}'). – c24w – 2016-10-12T19:14:37.420

It's not the sum for multiple monitors. It's the dimensions of a bounding box which contains all the monitors. – Paused until further notice. – 2019-05-17T20:25:56.947

xrandr --current | grep '*' | awk -v line="$SCREEN" 'NR==line{print $1}' | cut -d 'x' -f1 if you want to specify a screen (with a multi-monitor setup) (SCREEN is 1-indexed) – SapuSeven – 2019-10-25T10:10:43.190

5

You could use the xrandr -q command. From that you can create a shell script if needed.

For more information on the command go here or type man xrandr

ricbax

Posted 2010-10-06T14:33:05.710

Reputation: 4 894

2

#############################################
## I use this with a Video Recording Program.
#  window size --root option - information on the screen's root window
echo $(xwininfo -root | grep 'geometry' | awk '{print $2;}')
# output(s): 1024x768+0+0
#            height x width + x + y positions.
######################
## Reference Manual ##
man xwininfo

JimmyLandStudios

Posted 2010-10-06T14:33:05.710

Reputation: 121

I used xwininfo -root|sed '/Height/!d;s/.* //' for height and xwininfo -root|sed '/Width/!d;s/.* //' for width. – dessert – 2018-04-19T19:42:21.543

1

Two possible alternatives produced combining the answers of @user31752 and @eliezer-e-vargas

A simpler regex:

$ xrandr --current | sed -n 's/.* connected \([0-9]*\)x\([0-9]*\)+.*/\1x\2/p'
1440x900

or using cut:

$ xrandr --current | grep ' connected ' | cut -d ' ' -f 3 | cut -d '+' -f 1
1440x900

The use of grep '*' | uniq from @eliezer-e-vargas get a different line (ex. " 1440x900 59.90*+ 59.89" ) of xrandr output, while the grep ' connected ' get a simple one (ex. "LVDS1 connected 1440x900+0+0 .....").

The use of regex by @user31752 is nice, so the line that I'm using needs a simpler regex, or can be substituted whit the simpler cut command.

Example xrandr output

$ xrandr --current
Screen 0: minimum 320 x 200, current 1440 x 900, maximum 8192 x 8192
LVDS1 connected 1440x900+0+0 (normal left inverted right x axis y axis) 331mm x 207mm
   1440x900      59.90*+  59.89  
   1360x768      59.80    59.96  
   1152x864      60.00  
   1024x768      60.00  
   800x600       60.32    56.25  
   640x480       59.94  
VGA1 disconnected (normal left inverted right x axis y axis)
HDMI1 disconnected (normal left inverted right x axis y axis)
DP1 disconnected (normal left inverted right x axis y axis)

campisano

Posted 2010-10-06T14:33:05.710

Reputation: 141

Is there any reason for somebody to use these commands instead of the ones in Eliezer E. Vargas’s answer?

– Scott – 2017-05-09T01:54:04.110

Please [edit] that information into your answer. – Scott – 2017-05-09T03:07:25.967

1

Reading the Monitor Screen Data

The vesa standard provides a method of how to read the monitor screen resolution.

Extended Display Identification Data (EDID): This standard defines data formats to carry configuration information, allowing optimum use of displays.

A monitor typically supports multiple resolutions and refreshrates. Of course someone will prefer the maximum (physical) one.

To read this monitor data, try one of these solutions:

  • edid-decode

    If not installed, type

    sudo apt install edid-decode
    

    Then read the edid file

    edid-decode /sys/class/drm/card0-eDP-1/edid
    
  • read-edid

    Install with

    sudo apt install read-edid 
    

    Then read via i2c the screen monitor data and parse it

    sudo get-edid | parse-edid
    
  • Hexdump the edid data

    In case edid-tools are not installed, you can dump the edid hex-file, e.g.:

    hd /sys/class/drm/card0-eDP-1/edid
    

    To encrypt this hex file take a look at wiki or download the edid specifications.

abu_bua

Posted 2010-10-06T14:33:05.710

Reputation: 123

If you dont have X and xrandr installed, edid-decode is a perfect tool when you want to know the supported resolutions of the connected monitor! – UlfR – 2019-12-16T14:14:33.917

1

xdpyinfo will do it, with some parsing. It gives a lot of info which you'll then have to dig the screen number, and dimensions from

Rich Homolka

Posted 2010-10-06T14:33:05.710

Reputation: 27 121

0

As in the accepted answer but less complicated:

xdpyinfo | grep dimensions

Example of output:

dimensions:    1366x768 pixels (361x203 millimeters)

Billal Begueradj

Posted 2010-10-06T14:33:05.710

Reputation: 53