I have a directory filled with png files

$ tree ~/wallpaper/
├── foo--1366x768.png
├── foo--1920x1080.png
├── foo--2048x1080.png
├── foo--3440x1440.png
└── foo--3840x2160.png

0 directories, 5 files

I'm often changing monitors, sometimes with multiple attached. I want a script that detects the sizes of the monitors attached to my computer and sets the wallpaper from ~/wallpaper according to the monitor's size.

So, if monitors with resolutions 1366x768 and 3440x1440 are connected, I want the script to set the wallpapers to ~/wallpaper/foo--1366x768.png and ~/wallpaper/foo--3440x1440.png respectively. In this particular example, I know that issuing the command

feh --bg-fill ~/wallpaper/foo--1366x768.png ~/wallpaper/foo--3440x1440.png

will set the wallpapers correctly.

To solve the general problem, I know I can issue

xrandr | grep ' connected' | awk '{print $3}' | cut -f1 -d"+"

to get the dimensions of the monitors attached. In our running example, the output here is:


I want to now somewhow pipe this into a command to set the wallpaper with feh. How can I do this?

  • That would be easy with Bash and if you always had 2 monitors connected. Otherwise I'm not sure it's possible to do in a single line without --xinerama-index option. – Arkadiusz Drabczyk Sep 4 at 19:21
  • ...but I've just tried it and noticed that it blanks the other screen. man feh also mentions that. – Arkadiusz Drabczyk Sep 4 at 19:30

If you always had exactly 2 monitors connected that would be very easy to do with Bash:

#!/usr/bin/env bash

screen_size=$(xrandr | grep ' connected' | awk '{print $3}' | cut -f1 -d"+")

readarray -t <<<"$screen_size"
feh --bg-fill ~/wallpaper/foo--"${MAPFILE[0]}".png ~/wallpaper/foo--"${MAPFILE[1]}".png

But if number of connected monitors can vary, which is most probably the case in practice you could construct a feh command and then run it with eval like that:

#!/usr/bin/env bash

screen_size=$(xrandr | grep ' connected' | awk '{print $3}' | cut -f1 -d"+")

readarray -t <<<"$screen_size"
feh_command="feh --bg-fill"

for i in "${MAPFILE[@]}"
    echo size: "$i"
    feh_command="$feh_command ~/wallpaper/foo--$i.png"

eval "$feh_command"

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.