Last Updated: June 10, 2020
·
1.536K
· mparker17

Refer to someone else's home directory in bash and zsh

Anyone accustomed to working with *NIX systems knows that ~ refers to your home directory.

For example,

cd ~/Desktop # Change to your desktop folder.
echo ~ # Print your home folder
mysqldump > ~/Downloads/dump.sql # Writes to a file in your Downloads folder.

But, I just learned that it's possible to refer to someone else's home directory with ~username. For example, if your co-worker with the username bob has a database dump in their home directory, you can:

cp ~bob/dump.sql ~/ # Copy dump.sql from bob's home directory to your home directory.

Or, if you're trying to find the home directory of the deploy daemon (which has it's own user account), but you don't have admin privileges:

echo ~deploy # Print the deploy user's home directory.

This appears to work on both Linux and Mac OSX, in both ZSH and BASH.


Thanks to Joachim Sauer for posting this answer on Stackoverflow.