I am trying to create a script, which will change the wallpaper automatically when run.
#!/bin/bash
cd ~/
rm -r ~/.wallpaper
mkdir .wallpaper
cd ~/.wallpaper
wget https://source.unsplash.com/random/1920x1080
USER=$(whoami)
PATH="file:///home/$USER/.wallpaper/1920x1080"
echo $PATH
gsettings set org.gnome.desktop.background picture-uri "$PATH"
But when i do ./change_wallpaper.sh
I get the echo correctly, but then
./change_wallpaper.sh: line 12: gsettings: command not found
However, when I run the same command from terminal, it executes fine and wallpaper is getting changed.
When I run whereis gsettings
it tells
gsettings: /usr/bin/gsettings /usr/share/man/man1/gsettings.1.gz
Why is it showing gsettings: command not found
when I execute from script?
Your script won't work for everyone. The variable you set for home for your user will be incorrect for people who have their
HOME
location in a different place from/home/user
. For instance, my home location for my personal space is/home/user/l/j/ljames
.Instead of setting the path to be
"file:///home/$USER/.wallpaper/1920x1080"
you should more correctly change it to"file:///$HOME/.wallpaper/1920x1080"
. The variable$HOME
is already expanded to the full home space of the user.Your script will work if with these changes:
A more efficient example is:
The explaining of the lines are:
Because you change the
PATH
in your script. This reserved variable is used to locate executable files. Use another variable.Same with
USER
: it is reserved as well and already contains the current user, i.e. you do not need to setUSER=$(whoami)
.In general, when creating variables in shell scripts it is a good idea to use lowercase names. Usually, predefined variables (like
HOME
,USER
,PATH
) are all UPPERCASE and a simple way to avoid overwriting them is to use lowercase names in own scripts. Or use some prefix, e.g.MY_PATH
,MY_USER
etc.