I have this crontab file with a lot (100+) of jobs that execute some commands in the same directory, suppose ~/Documents/. Instead of cd ~/Documents/ && <command> for every jobs, I can use the cron HOME variable to change cron working directory, as explained in man crontab 5 (I've discovered this only recently).
However, if I set this in crontab:
PATH="/usr/local/bin:/usr/bin:/bin"
HOME=/home/user/Documents
every jobs that start with cd ~/Documents/ all of a sudden they don't work anymore. Same if the path is quoted and with/without last backslash.
After a lot of trial-and-error, I can say that is the ~ that doesn't work (it doesn't expand, I guess), because if I type the absolute path to Documents it works great. I've already tried to also add SHELL=/bin/bash.
I want to keep backward compatibility to all this kind of jobs so I can edit and test them one-by-one, and don't want to broke other jobs that have ~ in their command. What can I do?
HOME=/home/user/Documents- this doesn't look right. Normally $HOME is/home/username.~means$HOME. So when you setHOME=/home/user/Documents,~/Documentsexpands to/home/user/Documents/Documents. It's not a problem of it not expanding; it's just expanding to a different value than you want. This is part of why POSIX recommends you use lower-case names for your own variables (search for "reserved for applications"), so you don't unintentionally override ones likeHOMEorPATHthat reflect or modify how OS-provided tools work.