How to Find the Path of the Currently Logged In User’s Home Directory in Ubuntu

When writing bash scripts or executing commands in the Ubuntu terminal, you frequently need to reference files located in your personal home directory (such as configuration files in .config or SSH keys in .ssh). Hardcoding the absolute path (like /home/username/) is bad practice, as the script will instantly break if you run it as a different user or share it with a colleague.

Instead, Linux uses environment variables to dynamically store the path to the current user’s home directory. By calling this variable, you can ensure your commands and scripts always target the correct location, regardless of who is currently logged into the machine.

This guide explains how to find the path of the current user’s home directory in Ubuntu using the echo command.

Using the $HOME Environment Variable

The system automatically sets the $HOME environment variable the moment a user logs in. You can easily view its contents.

  1. Open your Ubuntu terminal.
  2. Type the following command:
    echo $HOME
  3. Press Enter.

The terminal will output the absolute path to your home directory, for example: /home/johndoe. If you run this exact same command while logged in as the root user, the output will dynamically change to /root.

Using the Tilde (~) Shortcut

While $HOME is excellent for scripting, it takes a few extra keystrokes to type out manually. For interactive terminal use, the Bash shell provides a much faster shortcut: the tilde (~) character.

The tilde is universally recognized by the shell as a direct alias for the $HOME variable.

  1. To verify this, type the following command:
    echo ~
  2. Press Enter.

The output will be identical to running echo $HOME (e.g., /home/johndoe).

Practical Applications

Understanding these shortcuts is critical for navigating the Linux file system efficiently.

  • Navigation: If you are buried deep within system folders (e.g., /var/log/nginx/) and want to instantly return home, you do not need to type cd /home/username. Simply type cd ~ (or even just cd by itself) and press Enter.
  • File Referencing: If you want to view a file on your desktop from anywhere in the system, you can type cat ~/Desktop/notes.txt rather than typing out the full absolute path.
  • Scripting: If you are writing a script that needs to create a backup file in the user’s home directory, you would write the output path as $HOME/backup.tar.gz.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.