build-a-shell / lesson-18.md
Lesson 18 · Words & expansion

Tilde expansion

A leading `~` is shorthand for your home directory. Today you add tilde expansion, a small rewrite that happens before a word is used as a path.

The goal

Expand a leading `~` in a word to the value of HOME; leave a tilde elsewhere in the word untouched.

Start here - the target
TO DO
Scenario: Expanding a leading tilde
GivenHOME is "/home/me"
Whenthe word "~/docs" is expanded
Thenit becomes "/home/me/docs"
Anda tilde that is not at the start of a word, like "a~b", is left unchanged
Background

The tilde is the shell’s shorthand for your home directory, so ~ on its own means /home/me and ~/docs means /home/me/docs. It is what lets you type cd ~/projects instead of spelling out the full path. Expansion replaces a leading ~ with the value of HOME, keeping whatever path follows.

The rule is deliberately narrow: the tilde only expands when it is the first character of a word and is followed by a / or nothing at all. A tilde in the middle of a word - a~b, an email address, a backup filename - is an ordinary character and stays put. (Real shells also expand ~user to that user’s home directory by looking it up in the password database; the plain ~ form is the one you reach for daily, and the one today’s spec pins down.)

Make it work
// only a tilde at position 0 of an unquoted word expands
if (word[0] == '~' && (word[1] == '\0' || word[1] == '/')) {
char *home = var_get(vars, "HOME");
// result = home + (word + 1)
}
CheckpointDONE
`cd ~/projects` and `ls ~` work. Commit and stop here.