How do I phrase a script that checks screen resolution with xrandr and changes resolution depending on what it is currently? Which is to say, toggles the resolution.

Sample output of xrandr -q

   1920x1080     59.98 +  59.97    59.96    59.93  
   1680x1050     59.95    59.88  
   1600x1024     60.17  
   1400x1050     59.98  
   1600x900      59.99*   59.94    59.95    59.82

How does it need to be phrased to check if the current resolution is 1600x900, then run xrandr -s 0, but if the current resolution is 1920x1080 run xrandr -s 4?

In short the question is how to make the script recognize the output of xrandr -q in order to toggle resolutions. For example:


if <current resolution is 1920x1080>
  xrandr -s 4
elif <current resolution is 1600x900>
  xrandr -s 0
 <do nothing>

Basically I need a means for the script to recognize which line the asterisk is on and switch it to the other one. How would that be accomplished?

1 Answer 1


Use process substitution:

#! /bin/bash

read resolution _rest < <(xrandr | grep '\*')

if [[ $resolution = 1920x1080 ]] ; then
    xrandr -s 5
elif [[ $resolution = 1600x900 ]] ; then
    xrandr -s 0
    echo Unknown resolution >&2
  • This does work as expected. This is a solution. Jun 29 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.