How do I find out my screen resolution from a shell script?
9 Answers
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.
-
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 usesed
again to get the two:WIDTH=$(echo $DIMENSIONS | sed -r 's/x.*//')
andHEIGHT=$(echo $DIMENSIONS | sed -r 's/.*x//')
.– mneriFeb 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.– mneriFeb 15, 2017 at 19:11 -
1
xdpyinfo | grep dimensions: | awk '{print $2}'
seems to be simpler and more readable.– x-yuriDec 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/…– PatSMay 4, 2022 at 17:02
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
-
-
-
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}')
.– c24wOct 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) Oct 25, 2019 at 10:10
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
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
############################################# ## 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
-
I used
xwininfo -root|sed '/Height/!d;s/.* //'
for height andxwininfo -root|sed '/Width/!d;s/.* //'
for width.– dessertApr 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
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
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)
-
Is there any reason for somebody to use these commands instead of the ones in Eliezer E. Vargas’s answer? May 9, 2017 at 1:54
-
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
fileedid-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.
-
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!– UlfRDec 16, 2019 at 14:14
As in the accepted answer but less complicated:
xdpyinfo | grep dimensions
Example of output:
dimensions: 1366x768 pixels (361x203 millimeters)