| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Where does $HOME come from?
 | 
| 4 | #
 | 
| 5 | # Usage:
 | 
| 6 | #   ./home-var.sh <function name>
 | 
| 7 | 
 | 
| 8 | set -o nounset
 | 
| 9 | set -o pipefail
 | 
| 10 | set -o errexit
 | 
| 11 | 
 | 
| 12 | # In the bash source, it appears to be set in a login shell?  But I don't seem
 | 
| 13 | # to be able to tickle that line.
 | 
| 14 | #
 | 
| 15 | # in variables.c, initialize_shell_variables():
 | 
| 16 | #
 | 
| 17 | # if (login_shell == 1 && posixly_correct == 0)
 | 
| 18 | #   set_home_var ();
 | 
| 19 |          
 | 
| 20 | test-bash() {
 | 
| 21 |   set +o errexit
 | 
| 22 | 
 | 
| 23 |   #local sh=bash
 | 
| 24 |   local sh=_tmp/spec-bin/bash
 | 
| 25 | 
 | 
| 26 |   local prog='echo home=$HOME'
 | 
| 27 |   env -i -- $sh -c "$prog"
 | 
| 28 |   env -i -- $sh -i -c "$prog"
 | 
| 29 |   env -i -- $sh --login -c "$prog"
 | 
| 30 |   env -i -- $sh --login -i -c "$prog"
 | 
| 31 |   env -i -- $sh --norc --login -c "$prog"
 | 
| 32 | 
 | 
| 33 |   local prog='echo HOME=; env | grep HOME'
 | 
| 34 |   # Test if exported
 | 
| 35 |   env -i -- $sh -c "$prog"
 | 
| 36 |   env -i -- $sh -i -c "$prog"
 | 
| 37 |   env -i -- $sh --login -c "$prog"
 | 
| 38 |   env -i -- $sh --login -i -c "$prog"
 | 
| 39 |   env -i -- $sh --norc --login -c "$prog"
 | 
| 40 | }
 | 
| 41 | 
 | 
| 42 | # ANSWER:
 | 
| 43 | 
 | 
| 44 | # https://unix.stackexchange.com/questions/123858/is-the-home-environment-variable-always-set-on-a-linux-system
 | 
| 45 | 
 | 
| 46 | # The POSIX specification requires the OS to set a value for $HOME
 | 
| 47 | #
 | 
| 48 | # HOME
 | 
| 49 | # The system shall initialize this variable at the time of login to be a
 | 
| 50 | # pathname of the user's home directory. See <pwd.h>.
 | 
| 51 | 
 | 
| 52 | # https://superuser.com/questions/271925/where-is-the-home-environment-variable-set
 | 
| 53 | # 
 | 
| 54 | # - by login on console, telnet and rlogin sessions
 | 
| 55 | # - by sshd for SSH connections
 | 
| 56 | # - by gdm, kdm or xdm for graphical sessions.
 | 
| 57 | #
 | 
| 58 | # This is annoying since the cron environment doesn't have it!  But it looks
 | 
| 59 | # like it shouldn't be fixed in the shell.
 | 
| 60 | 
 | 
| 61 | "$@"
 |