The $HOME Environment Variable
On Linux and other Unix-like systems, $HOME is an environment variable that normally contains the absolute path to the current user’s home directory. It is the standard location for personal files, application settings, shell configuration, and other user-specific resources.
A typical value might look like this:
/home/alice
For the root account, it is usually:
/root
The value is commonly established when the user account is created. On many systems, the default home-directory path is stored in the account database, commonly /etc/passwd, although services such as LDAP or another name-service directory can also provide it.
To display the value directly, use:
printf '%s\n' "$HOME"
You can also inspect it with printenv:
printenv HOME
If you need to compare the environment variable with the account database, use getent rather than parsing /etc/passwd manually:
getent passwd "$(id -u)"
The sixth field in the resulting record is the account’s default home directory.
Why It Matters
$HOME gives commands and applications a consistent way to locate user-specific resources. Instead of embedding a fixed path such as /home/alice, software can derive the location dynamically. This is especially important on systems with multiple users, where the same application may run under different accounts.
The shell’s tilde character, ~, provides a convenient shorthand for the home directory:
cd ~
cd "$HOME"
Both commands normally return the shell to the user’s home directory. The tilde is expanded by the shell, while $HOME is an ordinary environment variable expanded through parameter expansion. Quoting "$HOME" is recommended because the path could contain spaces or other shell-significant characters.
A home directory often contains hidden configuration files and directories:
~/.bashrc
~/.profile
~/.gitconfig
~/.ssh/
~/.config/
Applications use these locations to preserve settings between sessions. Modern Linux applications may also follow the XDG Base Directory specification, which places configuration under $XDG_CONFIG_HOME when it is set and otherwise defaults to $HOME/.config.
Everyday Usage
The most immediate use of $HOME is navigating to the home directory from anywhere:
cd "$HOME"
It also makes commands portable across user accounts. For example, the following command stores a file in the current user’s home directory:
cp report.pdf "$HOME/"
When writing shell scripts, avoid hard-coded user paths. The following version works only for a user named alice:
input="/home/alice/input.txt"
A better version adapts to the environment in which the script runs:
input="$HOME/input.txt"
For production scripts, you can even require the variable to be present:
: "${HOME:?HOME is not set}"
config_file="$HOME/.config/myapp/settings.conf"
This check fails early with a clear message rather than allowing the script to construct an invalid path later.
$HOME is also useful when creating directories:
mkdir -p "$HOME/backups"
mkdir -p "$HOME/.local/share/myapp"
Because many configuration files begin with a dot, they are hidden by default in directory listings. To include them, use:
ls -la "$HOME"
How It Differs from Other Paths
$HOME is related to several common path concepts, but each serves a different purpose.
| Variable or path | Purpose | Typical example |
|---|---|---|
$HOME |
Default home directory for the user | /home/alice |
$PWD |
Shell’s current working directory | /var/www/html |
$OLDPWD |
Previous working directory | /home/alice |
$PATH |
Directories searched for executable commands | /usr/local/bin:/usr/bin:/bin |
/etc |
System-wide configuration directory | /etc/passwd |
$PWD changes whenever the shell moves to another directory, while $HOME normally remains fixed for the lifetime of the session. $PATH, by contrast, determines where the shell searches for commands rather than where user data is stored.
A user’s home directory may itself be the current directory, but it does not have to be. For example, you might be working in /tmp while your personal configuration remains under $HOME.
Temporary Testing and Modification
For a short-lived test, the variable can be overridden in the current shell:
export HOME=/tmp/test-home
This affects the shell and any child processes it launches. It does not update the user account database or move existing files.
To override HOME for only one command, use a temporary assignment:
HOME=/tmp/test-home some-command
This form is safer than a persistent shell change because the original value remains intact after the command exits.
However, altering HOME can cause unexpected behavior. Programs may suddenly look for settings in a directory that does not contain them. This can affect:
- Shell startup files
- SSH configuration and keys
- Git settings
- Text editors and IDEs
- Package managers and development tools
- Applications that create caches or credential files
Some sudo configurations also replace HOME with root’s home directory. If a command behaves differently under sudo, compare both values:
printf 'User HOME: %s\n' "$HOME"
sudo printenv HOME
The behavior can be influenced by sudo policies such as always_set_home, so system-wide privilege management should be configured deliberately.
Changing an Account’s Home Directory
A permanent change should be made through the user-account system rather than by adding an export statement to .bashrc. Overriding HOME in a startup file may conceal the account’s real configuration path and break unrelated tools.
On many Linux systems, usermod can update the home-directory path and move the existing contents:
sudo usermod -m -d /new/home/alice alice
Before performing this operation, ensure that the destination’s parent directory exists and has appropriate permissions. Afterward, verify the recorded value:
getent passwd alice
The new value may not appear in the current login session because the shell inherited HOME when it started. The user should log out and log back in, or start a new session, so that all processes receive the updated environment.
It is also important to check applications with explicitly configured paths. For example, an SSH server may define an absolute AuthorizedKeysFile, while a service might store credentials outside the home directory. Changing the account’s home directory does not automatically update every application-specific setting.
Practical Guidelines
- Use
"$HOME"rather than an unquoted$HOMEin scripts. - Prefer
~or"$HOME"over hard-coded paths under/home. - Check that
HOMEexists before constructing critical paths. - Use a one-command assignment for temporary testing.
- Use
usermodfor a permanent account-directory change. - Remember that
HOMEis an environment value, not an immutable guarantee; programs should handle unusual or unset values gracefully.
In short, $HOME is more than a convenience for changing directories. It is a standard interface between user identity, filesystem organization, and application configuration. Understanding how it is set, expanded, and inherited helps create shell scripts that are portable and keeps everyday command-line workflows predictable.