Why Wallpaper is Replaced
Long story short, when you have desktop icons enabled, that essentially enables Nautilus to create the Desktop "window" with all the file and folder icons and manage background. feh
works with raw X11
desktop, i.e. bare bones, where no icons exist. So there's two potential solutions: one is to rely on Ubuntu's settings for background, or two - disable desktop icons. One can also make a script to launch both feh
and alter Ubuntu desktop settings, but that's redundant in my opinion. Whichever way you decide to go is up to you
Disabling/Enabling icons
This can be done via gsettings org.gnome.desktop.background show-desktop-icons false
to disable and org.gnome.desktop.background show-desktop-icons true
to enable. Once that's done Nautilus won't manage your desktop, hence won't set the background picture
Ubuntu Wallpaper Settings
Aside from obvious GUI methods, one can use gsettings
command to set the wallpaper.
gsettings set org.gnome.desktop.background picture-uri file:///home/my_user/Pictures/cool_wallpaper.png
Of course, previously mentioned caveat applies - the desktop icons have to be enabled.
Alternative to Your Script
I have noticed that you are trying to set a random wallpaper from a folder. You don't have to rely on feh
for that. In the past, I've written two scripts to do this task. One written as bash script and another one as Python script. Both scripts work with Ubuntu's gsettings
and alter the desktop wallpaper without disabling icons. Personally, I'd suggest the Python one as it is more polished.