LearnLinux FoundationsBash Shell Environment

Tilde Expansion

echo - a hands-on Linux lab on a real virtual machine.

The shell replaces a leading ~ with a home directory. Print your own home with ~, build a path with ~/Documents, reach another user home with ~root, and use ~+ and ~- for the current and previous directory. Tilde is the shorthand for $HOME, fires only at the start of a word, and stays literal inside double quotes, where you use $HOME instead. Home paths vary per machine, so the drills teach the shape and the byte-stable facts.

Your home directory is where you live on a Linux machine. Its full address might be /home/student, or /home/priya, or something longer. Every config file you own, every folder you make by default, sits somewhere under it. And you refer to it constantly.

Typing that full path every time would be slow and, worse, it would break the moment you moved to a machine where your home lives somewhere else. So the shell gives you a one-character shortcut that always means my home directory, whoever and wherever you are. That character is the tilde, ~, and this lesson is about the quiet trick the shell does with it.

The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. We use echo to do the checking. echo just prints back whatever you hand it, so it is the perfect way to SEE what the shell turned your ~ into before any real command uses it.

Before the shell runs your line, it reads it and quietly rewrites certain characters. When it sees a ~ at the start of a word, it swaps it out for the full path to your home directory. This swap is called tilde expansion, and it happens before the command ever runs.

You have met this idea already under another name. Your home path is stored in a variable called $HOME. Tilde is simply the shorthand: ~ expands to whatever $HOME holds. Think of ~ as a nickname for your home address the way a contact name is a nickname for a phone number. You say ~, the shell dials /home/student.

The cleanest way to watch this happen is echo, the command that prints its arguments. Hand it a lone tilde and it will print not the tilde, but the path the tilde became:

echo ~

prompt: student@linuxcamp:~$ answer: echo ~ output: /home/student hint: Type echo, a space, then a single tilde character: echo ~

You typed one character, ~, and the shell printed a full path, /home/student. That is tilde expansion caught in the act. The shell replaced ~ with the value of $HOME before echo ever saw it, so echo only ever received the finished path. This is also why bare cd and cd ~ do the same thing: both send you home, because ~ becomes your home path.

That path is this sandbox's home, /home/student, because the login user here is student. On YOUR machine the home path is whatever $HOME holds for your account, so echo ~ might print /home/priya, or /root, or something else entirely. The value differs; the behavior does not. ~ always expands to the current user's home, whatever that happens to be.

A lone ~ is useful, but the real power is building longer paths onto it. Put a slash and a name right after the tilde, with no space, and the shell expands the ~ and keeps the rest attached. So ~/Documents becomes your home path with /Documents glued on the end.

This is the form you will type most. It is how you write ~/.bashrc or ~/bin in scripts and configs and have them work for anyone. Point echo at a folder inside your home:

echo ~/Documents

prompt: student@linuxcamp:~$ answer: echo ~/Documents output: /home/student/Documents hint: Type echo, a space, then a tilde, a slash, and Documents, all joined: echo ~/Documents

Same rule as before: the /home/student part is this machine's home and yours will differ, but the MOVE is what matters. The shell expanded only the ~, then stitched /Documents on unchanged, giving /home/student/Documents. This is why ~/.bashrc in a config file is portable: it means the .bashrc in my home, no matter whose home that is. There is no hardcoded /home/name to break when the account changes.

Here is the rule that separates people who understand tilde expansion from people who get surprised by it. The shell only expands ~ when it is the FIRST character of a word. A tilde sitting in the middle of a word is left exactly as you typed it, a plain tilde, with no expansion at all.

Prove it to yourself. Put a tilde in the middle of some text and echo it back. The shell will not touch it:

echo file~backup

prompt: student@linuxcamp:~$ answer: echo file~backup output: file~backup hint: Type echo, a space, then file~backup as one word with the tilde in the middle: echo file~backup

It printed file~backup unchanged. Because the ~ was buried inside the word and not at its start, the shell treated it as an ordinary character and expanded nothing. This is the behavior you want: filenames and text that happen to contain a tilde are safe. Expansion is reserved for the one place it is useful, the very start of a word. It also fires right after a : in a list like $PATH, which is why home paths inside PATH expand too.

The tilde has a second trick. Follow it immediately with a user name and it expands to THAT user's home directory instead of yours. So ~root means the home directory belonging to the user root. This is one of the few outputs that is identical on nearly every Linux machine, because root's home is almost always /root.

Ask the shell where root lives:

echo ~root

prompt: student@linuxcamp:~$ answer: echo ~root output: /root hint: Type echo, a space, then a tilde stuck directly to the word root, no slash between them: echo ~root

The shell printed /root, root's home directory. By writing the user name straight after the tilde with no space, you told the shell expand to this person's home, not mine. This is how a script can point at another account's files without knowing the exact path. Unlike your own home path, /root is the same on almost every machine, which is why we can show its exact output here with confidence.

There are two more special forms worth knowing. ~+ expands to your current directory, the value of $PWD, and ~- expands to the previous directory you were in, the value of $OLDPWD. They are handy for jumping back to where you just were. The four recognized forms are the whole set: ~, ~username, ~+, and ~-. Anything else after the tilde is not a special form, and the next step shows what the shell does then.

This is where most people trip. Tilde expansion does NOT happen inside quotes. Wrap a tilde in double quotes and the shell leaves it completely alone, printing a literal ~ instead of your home path. The quotes tell the shell treat this as plain text, and the tilde is plain text again.

See it fail on purpose. Echo a tilde with double quotes around it:

echo "~"

prompt: student@linuxcamp:~$ answer: echo "~" output: ~ hint: Type echo, a space, then a tilde wrapped in double quotes: echo "~"

It printed a bare ~, not your home path. That is the trap. When you want the home path inside quotes, do not quote the tilde: either leave ~ unquoted, or use $HOME inside the quotes instead, since a variable like $HOME DOES expand inside double quotes. So echo "$HOME/logs" prints your home path with /logs attached, while echo "~/logs" would print the literal ~/logs. Rule of thumb: ~ unquoted, or $HOME when you must be inside double quotes.

Scaffolding off. No command is printed this time. You have every piece you need.

You want to confirm where the root user's home directory lives on this machine. Use echo to print the home path of the user named root, using the tilde shortcut rather than typing any path yourself. Remember: the user name goes directly after the tilde, with no space and no slash between them.

prompt: student@linuxcamp:~$ answer: echo ~root output: /root hint: Recall the ~username form. It is echo, then a tilde with the word root stuck straight onto it: echo ~root

That is the everyday move. The tilde opened the expansion, and root stuck directly to it told the shell to expand to that user's home instead of yours, giving /root. You recalled the ~username form from memory and typed it yourself, which is exactly the recall a real machine will ask of you. Because root's home is /root almost everywhere, this is one command whose answer you can trust across machines.

You earned this cheat sheet. Every row is a form of tilde expansion you just ran, seen through echo:

And the two rules that decide whether it fires at all: ~ expands ONLY at the start of a word (or right after a :), and it does NOT expand inside double quotes, where you use $HOME instead.

A tilde followed by a name that is not a real user is left completely alone. Type echo ~nouser and, because no user called nouser exists, the shell cannot find a home to expand to, so it prints the literal ~nouser unchanged. That is not an error message, it is the shell quietly saying I could not expand this, so I left it as text. The same calm rule as a middle-of-word tilde: when in doubt, the shell leaves your text alone.

The practice terminal has shown you the shape of tilde expansion. You can print your own home with ~, build a path onto it with ~/, reach another user's home with ~root, and you know the two rules that stop it, middle-of-word and double quotes. Every one of those you typed yourself.

The Bash Environment module ends with one real Linux machine, the Bash Environment capstone mission, and that is where you use ~ for real. A VM boots just for you, with its own live shell and its own home path. It hands you objectives that use exactly what you practiced across this module: variables, exports, the command lookups, aliases, quoting, and the tilde you just learned. One difference from here: the mission shows no commands. You read the objective, you recall the command, you type it. That recall is what makes it stick.

Finish the other Bash Environment lessons, then go find your way home on a real shell.

Practice Tilde Expansion in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.