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

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.


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

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