58

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

9 Answers 9

62
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.

4
  • 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
    Feb 15, 2017 at 16:57
  • 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
    Feb 15, 2017 at 19:11
  • 1
    xdpyinfo | grep dimensions: | awk '{print $2}' seems to be simpler and more readable.
    – x-yuri
    Dec 4, 2019 at 8:15
  • I was looking for something simpler that did not use XWindows, and found this answer for my needs. stackoverflow.com/questions/263890/…
    – PatS
    May 4, 2022 at 17:02
32

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
5
  • it says> xdpyinfo: Unable to open display "".
    – To Kra
    Dec 14, 2015 at 9:27
  • How do you find out the available modes to change to? Feb 8, 2016 at 6:32
  • 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
    Oct 12, 2016 at 19:14
  • It's not the sum for multiple monitors. It's the dimensions of a bounding box which contains all the monitors. May 17, 2019 at 20:25
  • 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
    Oct 25, 2019 at 10:10
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

4

A very simple method is to read out the modes file in the sys-directory:

cat /sys/class/graphics/*/modes

or respectively

cat /sys/class/graphics/*/virtual_size
1
  • I think this is the best answer for linux. Apr 26, 2022 at 22:01
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
2
  • I used xwininfo -root|sed '/Height/!d;s/.* //' for height and xwininfo -root|sed '/Width/!d;s/.* //' for width.
    – dessert
    Apr 19, 2018 at 19:42
  • geo=xwininfo -root | grep 'geometry' | awk '{print $2;}' s=${geo%+0+0} ; This is how my BASH script is for 'avconv -s $s' Mar 3, 2020 at 14:16
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

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)
2
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.

1
  • 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
    Dec 16, 2019 at 14:14
0

As in the accepted answer but less complicated:

xdpyinfo | grep dimensions

Example of output:

dimensions:    1366x768 pixels (361x203 millimeters)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .