Tilde paths

A path that starts with a tilde character (~) is interpreted specially by the shell, for tilde expansion into a system user's home directory. By itself, it refers to the current user's home directory:

$ echo ~
/home/bashuser
$ echo ~/important
/home/bashuser/important

In this circumstance, it checks the value of the HOME environment variable first if it can. However, if this variable is blank or if the tilde is followed by any valid system username, Bash will attempt to find the home directory for that user with reference to the system password file, usually /etc/passwd:

$ echo ~root
/root
$ echo ~root/.ssh
/root/.ssh

If the user does not exist, Bash will leave the tilde string the same, without raising an error:

$ echo ~notauser ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.