Use $HOME environment variable:

echo “$HOME”

This should work on most machines, but on some environments (this took place with me) this variable is not set. In this case you can use getent command:

getent passwd <username> | cut -d: -f6
getent passwd “$USER” | cut -d: -f6
getent passwd $(whoami) | cut -d: -f6
getent passwd $(id -un) | cut -d: -f6

The last two commands does not depend on any environment variables. For this to work you need getent installed.

But in rare case, if neither $HOME nor getent available, you can grep /etc/passwd file itself:

grep ^<username> /etc/passwd | cut -d: -f6

Change <username> with actual username or one of “$USER”, $(whoami) or $(id -un). This is the most reliable way.

Leave a Reply

Close Menu