I've got two outputs (side-by-side) and I'm using i3-wm. I would like to create shell script script, which runs:

# if output <BBB> is connected, but off
xrandr --output <BBB> --right-of <AAA> --mode 1920x1080

# if output <BBB> is connected, and on
xrandr --output <BBB> --off

I'm toggling monitor configuration to achieve better gaming performance (around 5-15% more FPS)

  • So basically what you're asking is how to find out from a shell script if a given display is connected or not, right? – Satō Katsura Oct 11 '16 at 17:55
  • Well, you're right - I'm asking how to find out if display/output is connected and on (or off). – kravemir Oct 11 '16 at 17:56

This ought to work:

xrandr --listactivemonitors | grep <BBB> >/dev/null && xrandr --output <BBB> --off || xrandr --output <BBB> --right-of <AAA> --mode 1920x1080


  • xrandr --listactivemonitors prints only the monitors that are currently on.
  • grep <BBB> >/dev/null Searches the previous output for the name of the monitor we want to toggle. If it is found, grep will return an exit code that the shell interprets as true. If it is not found, it will return an exit code that the shell interprets as false. The output is sent to /dev/null to avoid cluttering the screen.
  • && xrandr --output <BBB> --off If grep found the monitor in the list of active monitors, then this will run, turning the monitor off. But if grep exited with a falsey exit code, then this will not run because no matter what it evaluates to, the logical and clause as a whole is already known to be false.
  • || xrandr --output <BBB> --right-of <AAA> --mode 1920x1080 If grep did not find it, then this clause will run, turning the monitor on. It runs because the previous clause (grep ... && xrandr ...) evaluated to false. In order to know if this logical or clause is true, the shell must evaluate the right hand side. On the other hand, if the left-handside has already evaluated to true, then there is no need to evaluate the right-hand side and so this will not be executed.

Here is a succinct write up on control flow mediated by logical conditionals.

  • Would you please explain how this works?  Do not respond in comments; edit your answer to make it clearer and more complete. – Scott Aug 31 '17 at 19:46
  • 1
    One limitation of this answer is that both outputs need to be connected. Thus, in case of external screen is ON and then disconnecting it, you cannot change it back. My solution is to prepend an additional check, if the external screen is connected at all: xrandr | grep 'VGA1 disconnected' > /dev/null && xrandr --output LVDS1 --auto || xrandr --listactivemonitors | grep VGA1 > /dev/null && xrandr --output VGA1 --off --output LVDS1 --auto || xrandr --output VGA1 --auto --output LVDS1 --off NOTE: VGA1 is my external outlet. – Dennis Nov 18 '17 at 10:40

If you want to avoid having a tmpfile around when not absolutely necessary, try this (LVDS-1: laptop display, VGA-1: external monitor):


switchDisplay() {
  /usr/bin/xrandr --auto && /usr/bin/xrandr --output "$1" --off

case "$0" in
  switchDisplay "VGA-1";
  switchDisplay "LVDS-1";
  ### Debian Buster xrandr man page is missing critical information.
  ### xrandr --listactivemonitors
  ### Monitors: 1
  ###  0: +VGA-1 1920/509x1080/286+0+0  VGA-1
  /usr/bin/xrandr --listactivemonitors | /bin/grep -q "VGA-1" 1>/dev/null 2>&1
  if test "$?" -eq 0; then
    switchDisplay "VGA-1"; ### VGA active, turn it off.
    switchDisplay "LVDS-1"; ### VGA inactive, turn it on.

Script is called ext-on in $PATH (i.e. ~/.local/bin) and ext-off, ext-toggle are symlinks to ext-on. Adapt to your needs.

Convenience for i3wm users w/ a Fn+-key combo, add to ~/.config/i3/config:

bindsym XF86Display             exec --no-startup-id ~/.local/bin/ext-toggle

Works like a charm. Never let me down ;)



Please check this link:


The script is written in bash and gives as well how to set a key shortcut on you i3 config file.

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.