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?
Best Answer
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.