Skip to content

Instantly share code, notes, and snippets.

@senorihl
Last active May 12, 2026 14:00
Show Gist options
  • Select an option

  • Save senorihl/98ecc6e0bd38e4b6b6f2bad09cf022d4 to your computer and use it in GitHub Desktop.

Select an option

Save senorihl/98ecc6e0bd38e4b6b6f2bad09cf022d4 to your computer and use it in GitHub Desktop.
Change Ubuntu wallpaper from CLI

Bash script to change Ubuntu background image from CLI with random Picsum image

Installation

wget https://gist.githubusercontent.com/senorihl/98ecc6e0bd38e4b6b6f2bad09cf022d4/raw/picsum-wallpaper.sh -O $HOME/picsum-wallpaper
chmod a+x $HOME/picsum-wallpaper 

Usage

~/picsum-wallpaper
#!/usr/bin/env bash
DRY=false
VERBOSE=false
WIDTH=$((1920*2))
HEIGHT=$((1080*2))
print_message () {
echo -e "[$(date -u +"%Y-%m-%dT%H:%M:%SZ")] \033[36m$1\033[0m" || true
}
command () {
local __resultvar=$2
local command_to_run=$1
[[ "$VERBOSE" = "true" ]] && print_message "$command_to_run" || true
if [[ "$DRY" = "false" ]]; then
if [[ "$__resultvar" ]]; then
eval "$__resultvar=\`$command_to_run\`"
else
eval $command_to_run
fi
fi
}
command "pgrep gnome-session | head -1" GNOME_PID
command "echo \"/proc/$GNOME_PID/environ\"" PROC_FILE
command "echo \"\$(grep -z DBUS_SESSION_BUS_ADDRESS $PROC_FILE|cut -d= -f2-)\"" BUS_ADDRESS
command "export DBUS_SESSION_BUS_ADDRESS=$BUS_ADDRESS"
command "gsettings get org.gnome.desktop.background picture-uri | sed \"s/file:\\\/\\\///g\"" previous_image
print_message "Current image is $previous_image"
URL=https://picsum.photos/$WIDTH/$HEIGHT.jpg
command "echo '$HOME/Images/picsum-$(date -u +"%Y-%m-%dT%H:%M:%SZ").jpg'" next_image
print_message "Next image will be downloaded from $URL to $next_image"
command "wget -qO- '$URL' > '$next_image'"
command "gsettings set org.gnome.desktop.background picture-uri 'file://$next_image'"
if [[ $previous_image == *"$HOME/Images"* ]]; then
print_message "Removing old image $previous_image"
command "rm -rf $previous_image"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment