Dependency on HOME which may not be set #27

Closed
opened 2025-12-29 00:22:32 +01:00 by adam · 1 comment
Owner

Originally created by @samueldr on GitHub (Dec 31, 2015).

The current script uses ${HOME}, when in some cases it might not be set in the environment. The use case where it happens is using letsencrypt.sh as a systemd service/timer set.

This causes this issue:

letsencrypt.sh[]: [...]/letsencrypt.sh: line 30: HOME: unbound variable

As this is used to find plausible config file locations, I wouldn't know what's the best way to avoid this issue. The ${HOME} location might be important for some cases, for mine it isn't.

As a stop-gap solution (for anybody setting up a systemd timer), it can be set explicitly.

[...]
[Service]
Environment="HOME=/root/"
[...]

Personally, I would use ~ instead of ${HOME} which does always resolve to the current user's home, even when neither ${HOME} or ${USER} are in the environment. There is one caveat with the use of ~, its properties around quoting are different than those of variables. I have not tested with a folder with spaces as a home folder, but I would guess that it would be even rarer than an execution of the script with a barebones environment. I think that with the current use, the quoting would still be fine, as long as it is used like so, unquoted: ~/.letsencrypt.sh.


Here's a test case: /usr/local/bin/tildeexpansion.sh

#!/bin/bash

unset HOME

set -e
set -u

echo ~
echo ~/.letsencrypt.sh

echo "${HOME:-$(echo ~)}"
echo "${HOME:-$(echo ~)}/.letsencrypt.sh"

echo "${HOME:-~}"
echo "${HOME:-~}/.letsencrypt.sh"

echo "${HOME}"
echo "${HOME}/.letsencrypt.sh"

To run as a systemd service: /etc/systemd/system/tildeexpansion.service

[Unit]
Description=Tests tilde expansion

[Service]
Type=oneshot
ExecStart=/usr/local/bin/tildeexpansion.sh

It can be run with sudo systemctl daemon-reload; sudo systemctl start tildeexpansion. /usr/local/bin/tildeexpansion.sh needs to be executable. The output can be looked at using sudo journalctl --unit tildeexpansion. This test case service will fail since the exit code of the script is a failure.

References:

Originally created by @samueldr on GitHub (Dec 31, 2015). The [current script](https://github.com/lukas2511/letsencrypt.sh/blob/f090fbcf7f773c82676e54e37336cffe2fbd7139/letsencrypt.sh#L41) uses `${HOME}`, when in some cases it might not be set in the environment. The use case where it happens is using letsencrypt.sh as a systemd service/timer set. This causes this issue: ``` letsencrypt.sh[]: [...]/letsencrypt.sh: line 30: HOME: unbound variable ``` As this is used to find plausible config file locations, I wouldn't know what's the best way to avoid this issue. The `${HOME}` location _might_ be important for some cases, for mine it isn't. As a stop-gap solution (for anybody setting up a systemd timer), it can be set explicitly. ``` [...] [Service] Environment="HOME=/root/" [...] ``` Personally, I would use `~` instead of `${HOME}` which does always resolve to the current user's home, even when neither `${HOME}` or `${USER}` are in the environment. There is one caveat with the use of `~`, its properties around quoting are different than those of variables. I have not tested with a folder with spaces as a home folder, but I would guess that it would be even rarer than an execution of the script with a barebones environment. I think that with the current use, the quoting would still be fine, as long as it is used like so, unquoted: `~/.letsencrypt.sh`. --- Here's a test case: `/usr/local/bin/tildeexpansion.sh` ``` #!/bin/bash unset HOME set -e set -u echo ~ echo ~/.letsencrypt.sh echo "${HOME:-$(echo ~)}" echo "${HOME:-$(echo ~)}/.letsencrypt.sh" echo "${HOME:-~}" echo "${HOME:-~}/.letsencrypt.sh" echo "${HOME}" echo "${HOME}/.letsencrypt.sh" ``` To run as a systemd service: `/etc/systemd/system/tildeexpansion.service` ``` [Unit] Description=Tests tilde expansion [Service] Type=oneshot ExecStart=/usr/local/bin/tildeexpansion.sh ``` It can be run with `sudo systemctl daemon-reload; sudo systemctl start tildeexpansion`. `/usr/local/bin/tildeexpansion.sh` needs to be executable. The output can be looked at using `sudo journalctl --unit tildeexpansion`. This test case service _will_ fail since the exit code of the script is a failure. References: - http://mywiki.wooledge.org/BashPitfalls#echo_.22.2BAH4.22 - http://mywiki.wooledge.org/BashPitfalls#export_foo.3D.2BAH4-.2Fbar - https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html
adam closed this issue 2025-12-29 00:22:32 +01:00
Author
Owner

@lukas2511 commented on GitHub (Jan 8, 2016):

I completely removed using the config from a home directory in ff11639624, but thanks for explaining the workaround.

Users who still want to use this can add an alias to their shells config to call the script with --config /path/to/config.

@lukas2511 commented on GitHub (Jan 8, 2016): I completely removed using the config from a home directory in ff11639624047e0b697d42ee06d1e93132310447, but thanks for explaining the workaround. Users who still want to use this can add an alias to their shells config to call the script with `--config /path/to/config`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/dehydrated#27