I wanted to make a bash script that takes a random picture from a folder and sets it as the desktop background. I figured out the algorithm (it's not hard) but I can't find a command that would change the background picture if you give it the path to the picture.

I found gconftool for bash 2.~ and gsettings for bash 3.~, but i have GNU bash, version 4.3.42(1)-release on Ubuntu 16.04 LTS and these don't work.

  • What command did you use ? you can set a bg image with several tools from feh to nitorgen - – Panther Jun 6 '16 at 16:01
  • gconftool and gsettings aren't dependent on the version of bash, and nobody uses anything older than bash 4 on any current Ubuntu system. – muru Jun 6 '16 at 16:02

Assuming you're using standard Ubuntu 16.04 with Unity, you can use the following command to set the wallpaper:

gsettings set org.gnome.desktop.background picture-uri "file:///home/username/path/to/image.jpg"

You should also look at Variety wallpaper changer - it does the same thing as what you want to do with a shell script, with a lot of features and options.

  • 1
    I just typed in this command without puting file://... in "" and it worked! This entire time the quotation marks were being the problem – someoneb100 Jun 6 '16 at 16:19
  • Working as described with quotation marks in 16.04. – Chaim Eliyah May 17 '17 at 2:02
  • for me, it worked with file:///... but without any quotes. Tested on Ubuntu 20.04 and CentOS 8 – xCovelus Apr 6 at 15:39

I've done it like this:


function get_next_photo() {
    # Returns a random file form targetdir
    files=( "$targetDir"/* )
    echo "${files[RANDOM % ${#files[@]}]}"

function set_background() {
    # Takes an absolute file path as argument. Need * for spaces in path
    echo "Setting background to $bg"
    gsettings set org.gnome.desktop.background picture-uri "file://$bg"

echo "Next background is $background"
set_background $background

Then add a quick launcher for your script to the dock, and you can change your wallpaper in one click.

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