Tilde Expansion and Home Directories
Tilde expansion lets you write ~ as a short way to name a home directory. When Bash sees an unquoted ~ at the start of a word, it replaces it with the path of your home directory before the command runs, so ls ~/Documents becomes ls /home/ana/Documents. The same feature can name another user's home directory (~root) and the current and previous working directories (~+ and ~-).
Understanding the Concept
Every user account on a Linux or macOS system has a home directory, the folder where that user keeps personal files and settings. The path differs from user to user and from system to system: /home/ana on many Linux machines, /Users/ana on macOS, /root for the administrator account on most Linux systems. Typing the full path would make commands and scripts depend on one particular machine.
The tilde solves this. It stands for "the home directory of whoever is asking", so the same command works for every user. The important detail is that the expansion is done by the shell, not by the command. The command never sees the tilde:
echo ~
echo ~/notes.txt
Output (the home directory in these examples is /home/ana):
/home/ana
/home/ana/notes.txt
echo ~received the single argument/home/ana.- In
~/notes.txt, the tilde was replaced and the rest of the word,/notes.txt, was kept as it was. - Neither command checked whether the file
notes.txtexists. Tilde expansion only builds text.
The Forms of Tilde Expansion
| Form | Expands to | Where the value comes from |
|---|---|---|
~ | Home directory of the current user | The HOME variable |
~/path | The home directory followed by /path | The HOME variable |
~name | Home directory of the user called name | The system's user database |
~+ | The current working directory | The PWD variable |
~- | The previous working directory | The OLDPWD variable |
The first three forms are available in every POSIX shell. ~+ and ~- are Bash extensions.
How Bash Finds the Tilde Prefix
Bash looks at each word of a command. If the word starts with an unquoted tilde, the characters from the tilde up to the first unquoted slash (or the end of the word) form the tilde prefix. The characters after the tilde in the prefix are treated as a login name.
- If the prefix is only
~, Bash uses the value ofHOME. - If the prefix is
~name, Bash looksnameup as a user account. - If the lookup fails, the word is left exactly as written.
- If the lookup works, the prefix is replaced by the path, and the rest of the word is attached to it.
Because the prefix ends at the first slash, ~/projects/demo has the prefix ~, and ~root/.profile has the prefix ~root.
When Tilde Expansion Does Not Happen
Three situations leave the tilde alone.
The tilde is quoted. Quotes and backslashes turn off the special meaning:
echo "~"
echo '~/notes.txt'
echo \~
echo ~"/notes"
Output:
~
~/notes.txt
~
~/notes
The last command shows that even a quote placed directly after the tilde stops the expansion. The tilde prefix must contain no quoted characters.
The tilde is not at the start of a word. A tilde in the middle of a word is an ordinary character:
echo a~
echo a~/b
echo x~root
Output:
a~
a~/b
x~root
The name does not match any user. A word such as ~nosuchuser_xyz is printed as it was typed:
echo ~nosuchuser_xyz
Output:
~nosuchuser_xyz
Tilde Expansion in Variable Assignments
Assignments get one extra rule. In an assignment, Bash also expands a tilde that appears right after the = sign and right after every colon in the value. This is what makes lists of directories such as PATH convenient to write:
P=~/bin
echo "$P"
p=/usr/bin:~/bin:~/tools
echo "$p"
Output:
/home/ana/bin
/usr/bin:/home/ana/bin:/home/ana/tools
Both tildes in the second value were expanded, because each one follows a colon (or the equals sign). A common use is adding a personal directory to the front of PATH:
PATH=/usr/bin:/bin
export PATH=~/bin:$PATH
echo "$PATH"
Output:
/home/ana/bin:/usr/bin:/bin
Quoting the value switches this off, because the tilde is then inside quotes:
Q="~/bin"
echo "$Q"
Output:
~/bin
A subtle point is that Bash applies the assignment rule to words that only look like assignments when they are command arguments:
echo foo=~/bin
echo --dir=~/bin
Output:
foo=/home/ana/bin
--dir=~/bin
The first word has the shape name=value, so Bash treated the part after = as an assignment value. The second word starts with --dir, which is not a valid variable name, so its tilde was left alone. A script that needs the path in an option like --dir= should build it with a variable: --dir="$HOME/bin".
Home Directories of Other Users
Writing a user name directly after the tilde gives that user's home directory, as recorded in the system's user database:
echo ~root
echo ~root/.profile
Output (on most Linux systems):
/root
/root/.profile
The paths printed depend on the machine. The administrator's home directory is /root on most Linux systems but differs on other systems such as macOS.
The Current and Previous Directory: ~+ and ~-
Bash keeps the path of the current directory in PWD and of the previous one in OLDPWD. The forms ~+ and ~- expand to those values:
cd /tmp
cd /var
echo ~+
echo ~-
Output:
/var
/tmp
~+ is the same as $PWD, and ~- is the same as $OLDPWD. Before any cd has happened, OLDPWD is unset, and ~- is then left unchanged as the text ~-.
The command cd - uses the same idea. It changes to the previous directory and prints its path.
Where the Home Directory Comes From
For a plain ~, Bash reads the HOME variable. If you change HOME, the tilde follows:
HOME=/srv/demo
echo ~
echo ~/x
Output:
/srv/demo
/srv/demo/x
If HOME is unset, Bash does not give up. It falls back to the home directory recorded for the current user in the system's user database. For the administrator account in a typical Linux system, echo ~ prints /root even after unset HOME.
The result of a tilde expansion is treated as if it were quoted, so it is never split into several words, even if the directory name contains a space:
HOME='/home/my user'
printf '[%s]\n' ~
printf '[%s]\n' ~/x
Output:
[/home/my user]
[/home/my user/x]
Each command received one argument.
Why a Variable Cannot Build a Tilde Prefix
Tilde expansion happens early, before Bash expands variables. This order has a visible effect: a variable's value cannot be used to make a tilde prefix.
user=root
echo ~$user
eval echo ~$user
Output:
~root
/root
When Bash processed ~$user, the word did not yet contain a valid user name; it contained the text $user, which is not a login name. The tilde was left alone, and the variable was expanded afterwards, which produced the text ~root. By then the tilde step had already finished. The eval version works because eval runs the whole line through the expansions a second time, but eval carries serious risks that a later lesson on security explains. A safer alternative is to ask the system for the directory with a command such as getent passwd root, or simply to avoid building user paths from variables.
Tilde Compared with $HOME
For the current user's home directory, ~ and $HOME give the same result. They differ in where they work:
- A tilde is expanded only when it is unquoted and at the start of a word (or after
=or:in assignments). $HOMEis expanded in most places, including inside double quotes.
This matters for paths that contain spaces. Inside double quotes the tilde does not expand, but $HOME does:
printf '[%s]\n' "$HOME/my docs"
printf '[%s]\n' ~/"my docs"
printf '[%s]\n' "~/my docs"
Output:
[/home/ana/my docs]
[/home/ana/my docs]
[~/my docs]
The first command quotes everything and uses $HOME. The second leaves the tilde outside the quotes and quotes only the part with the space, so the path stays one word. The third quotes the tilde as well, so nothing is expanded.
Practical Applications
Combining Tilde with Wildcards
Tilde expansion runs before filename expansion, so a home directory can be combined with a wildcard:
echo ~/*.txt
Output (if the home directory contains a.txt and b.txt):
/home/ana/a.txt /home/ana/b.txt
Bash first replaced ~ with the home path and then expanded the pattern against the files in that directory.
Moving Around and Copying Files
cd ~
cd
cp report.txt ~/backup/
mkdir -p ~/projects/demo
cd ~and plaincdboth change to the home directory.cp report.txt ~/backup/copies a file into a folder in the home directory, from any current directory.mkdir -p ~/projects/democreates a nested path under the home directory.
Using $HOME in Scripts
Inside scripts, paths are often built with variables and quotes, which makes $HOME the natural choice:
backup_dir="$HOME/backups"
mkdir -p "$backup_dir"
echo "Saving to $backup_dir"
Output:
Saving to /home/ana/backups
The same line written with a tilde in quotes, "~/backups", would have produced the literal text ~/backups, creating a directory named ~ in the current folder.
Availability in Other Shells
~ and ~name work in every POSIX shell, including Dash, the shell that /bin/sh points to on Debian and Ubuntu. The extensions ~+ and ~- are Bash features. In Dash, echo ~+ prints the text ~+.