Bash Quick Start Guide
上QQ阅读APP看书,第一时间看更新

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
~notauser

If you want to print a tilde for a real user without expanding it, you need to quote it. Escaping, single-quoting, and double-quoting all work:

$ echo ~bashuser
/home/bashuser
$ echo \~bashuser '~bashuser' "~bashuser"
~bashuser ~bashuser ~bashuser