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 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 '17 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 '17 at 19:11
-
1`xdpyinfo | grep dimensions: | awk '{print $2}'` seems to be simpler and more readable. – x-yuri Dec 04 '19 at 08:15
-
I was looking for something simpler that did not use XWindows, and found this answer for my needs. https://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window – PatS May 04 '22 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
- 110
- 1
- 4
- 321
- 3
- 3
-
-
-
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 '16 at 19:14
-
It's not the sum for multiple monitors. It's the dimensions of a bounding box which contains all the monitors. – Dennis Williamson May 17 '19 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 '19 at 10:10
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
- 369
- 5
- 7
#############################################
## 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
- 121
- 4
-
I used `xwininfo -root|sed '/Height/!d;s/.* //'` for height and `xwininfo -root|sed '/Width/!d;s/.* //'` for width. – dessert Apr 19 '18 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' – JimmyLandStudios Mar 03 '20 at 14:16
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)
- 421
- 5
- 4
-
Is there any reason for somebody to use these commands instead of the ones in [Eliezer E. Vargas’s answer](https://superuser.com/q/196532/150988#338697)? – Scott - Слава Україні May 09 '17 at 01: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-decodeThen read the
edidfileedid-decode /sys/class/drm/card0-eDP-1/edidread-edid
Install with
sudo apt install read-edidThen read via i2c the screen monitor data and parse it
sudo get-edid | parse-edidHexdump the edid data
In case edid-tools are not installed, you can dump the
edidhex-file, e.g.:hd /sys/class/drm/card0-eDP-1/edidTo encrypt this hex file take a look at wiki or download the edid specifications.
- 369
- 5
- 7
-
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 '19 at 14:14
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
- 31,057
- 6
- 55
- 80
As in the accepted answer but less complicated:
xdpyinfo | grep dimensions
Example of output:
dimensions: 1366x768 pixels (361x203 millimeters)
- 295
- 2
- 4
- 18