HOME variable is not set

Read this question on stackoverflow.com and this answer by user grawity on superuser.com

You should not use $HOME in init.d, because it is not clear which users home to use, until this user logs in.

Quote from POSIX specification:

HOME
The system shall initialize this variable at the time of login to be a pathname
of the user's home directory. See <pwd.h>.

You can use little hack, to get home folder of user myuser in your script

su - myuser -c /usr/bin/env | grep HOME

It is better to use script below, because usually there can be other HOME_* folders. Such as JAVA_HOME etc.

su - myuser -c /usr/bin/env | grep "^HOME="

Seems like the $HOME is being interpreted before it goes to bash? I would try one of the following. Either add:

env HOME=/home/MyName

to the code just after the description.

Or move the code from inside the script block to another script file with:

#! /bin/bash

as line 1. Then have

exec /path/to/my/script.sh

If all you need is the HOME variable to be set (which solved my problem), you can explicitly set it with the env directive:

env HOME=/home/user

via https://askubuntu.com/a/1125244/160358