I'd like to override the user's HOME location locally in a script in order to force an application to store/load its settings portably.

export HOME=$TMPDIR/Data

These works in most cases, but there are a few apps that still find the "real" home path.

Why is my method of overriding the user's HOME location not working?

  • 3
    What are these applications ? – jlliagre Jul 3 '13 at 22:12
  • bsnes, pSX, (possibly others i don't remember now) – eadmaster Jul 3 '13 at 23:54

Unfortunately this is highly dependent on what your application is. There are multiple ways that an application can gleam a user's home directory.

  1. $HOME

    You're already aware of this one. $HOME is an environment variable that can be overridden through login config. files such as .bashrc or .bash_profile in a Bash environment. The value is usually something like /home/username.

  2. /home/$USER

    I'd consider this a variant of $HOME, but I've encountered applications that foolishly assume that a user's home directory has to be under /home, so they tack on the variable $USER and use that.

  3. parsing /etc/passwd

    This is probably the worst offender in terms of the worst way to get a user's home directory but lots of tools and scripts do it this way. Most of them are a variance of something like this where they use the getent command:

    $ getent passwd | grep $1 | awk -F: '{ print $6 }'
    # for example (user "saml")
    $ getent passwd | grep saml | awk -F: '{ print $6 }'

    The backend "database" that stores the user's info can be anything from the /etc/passwd file, to NIS, to an LDAP database.

  4. GTK & g_get_home_dir()

    The GTK library provides a API, g_get_home_dir() which will pull user info from the "passwd" backend rather than use $HOME.

    excerpt from GLib Reference Manual

    Note that in contrast to traditional Unix tools, this function prefers passwd entries over the HOME environment variable.

  5. KDE

    KDE appears to make use of the $HOME. It will first look for KDEHOME , falling back to $HOME/.kde if that is not set.

    excerpt KDE System Administration/KDE Filesystem Hierarchy

    This directory tree is, as the name suggests, normally located in the user's home directory. If this environment variable is not defined, the default location $HOME/.kde4 is used.

  • what about python, java, and mono apps? – eadmaster Jul 14 '13 at 14:06
  • @eadmaster - The technology (python, java, etc.) isn't what controls this. It's like asking why do different C++ applications not use $HOME the same way. It depends on how the programmer implemented their app. – slm Jul 14 '13 at 18:00

There are two ways to get the current user's home directory.

  • One way is to look at the user's passwd entry (in /etc/passwd).
  • Another way is to look at the HOME environmental variable. $HOME is actually set from the passwd entry. Applications that may set it include your terminal emulator, your login manager, and sudo.

The applications that you are unable to fool may be looking at the user's passwd entry. It is also possible that they are storing the user's home directory somewhere (possibly as part of an absolute path to some file or directory) and ignoring both HOME and the passwd entry.

One way to check if an application uses the HOME environmental variable is to grep for it. For example, grep HOME /usr/bin/pdftex will return Binary file /usr/bin/pdftex matches, meaning pdftex might use $HOME.

For more definite answers, look through the application's source code.
Use grep -r 'getenv\s\?(.*HOME' to speed up the source code analysis a bit.

  • Also take a look at this post. – user26112 Jul 3 '13 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.