Chapter 1: Bash Home
Bash Home — or more precisely, what “$HOME” means in Bash, and also the famous ~ (tilde) that people use all the time.
Think of your computer like a big apartment building. Every person (every user) gets their own private apartment. That apartment is called your home directory.
In Bash (and in Linux/macOS in general), your personal apartment has two very common names/ways to refer to it:
- $HOME → the official, polite name (an environment variable)
- ~ → the short nickname (a special Bash shortcut)
Both almost always point to the exact same place — usually something like /home/webliance on your system in Hyderabad.
Let me explain it like we’re sitting together looking at your terminal.
1. What exactly is the “home directory”?
When you log in to Linux / Ubuntu / macOS / Git Bash / WSL etc., the system says:
“Welcome back Webliance! Go sit in your room → /home/webliance”
This folder (/home/your-username) is special because:
- You have full permission to create/delete/change anything inside it
- Most of your personal files live here (Documents, Downloads, Desktop, Pictures…)
- Hidden configuration files for programs live here (.bashrc, .gitconfig, .ssh/, etc.)
- When you open a new terminal → it automatically starts inside your home directory
Try this right now in your terminal:
|
0 1 2 3 4 5 6 |
pwd |
You will probably see something like:
|
0 1 2 3 4 5 6 |
/home/webliance |
or on macOS maybe
|
0 1 2 3 4 5 6 |
/Users/webliance |
or in Git Bash on Windows
|
0 1 2 3 4 5 6 |
/c/Users/webliance |
That path = your home directory.
2. How Bash gives you two easy ways to say “my home”
Way 1: The variable → $HOME
$HOME is a normal environment variable (like a sticky note the system wrote for you).
Try these:
|
0 1 2 3 4 5 6 |
echo $HOME |
→ /home/webliance (or whatever your actual path is)
|
0 1 2 3 4 5 6 |
echo "My important files are in $HOME/Documents" |
→ My important files are in /home/webliance/Documents
|
0 1 2 3 4 5 6 7 |
cd $HOME pwd |
→ takes you home and shows the path
Very safe and clear — especially inside scripts!
Way 2: The shortcut → ~ (tilde key — usually left of 1 key)
Bash has a special rule: whenever it sees ~ (not inside quotes), it replaces it with your home path automatically.
Try:
|
0 1 2 3 4 5 6 |
echo ~ |
→ same as echo $HOME
|
0 1 2 3 4 5 6 7 |
cd ~/Desktop pwd |
→ /home/webliance/Desktop
|
0 1 2 3 4 5 6 |
ls ~/Downloads |
→ shows files in your Downloads folder (without typing full path)
Super short and convenient when typing commands by hand!
3. Important differences between $HOME and ~
| Feature | ~ (tilde) | $HOME |
|---|---|---|
| Type | Shell expansion (Bash magic) | Normal environment variable |
| Works inside double quotes? | No → “today ~” prints “today ~” | Yes → “today $HOME” works |
| Works inside single quotes? | No → ‘go to ~’ prints ‘go to ~’ | No → ‘go to $HOME’ prints literally |
| Works in scripts? | Usually yes, but can be tricky | Always safe and recommended |
| Can refer to other users? | Yes → ~rahul (home of user rahul) | No (only your own home) |
| What if HOME is unset? | Bash still knows from /etc/passwd | Empty or error |
| Best for typing fast? | Yes | No (longer) |
| Best for writing scripts? | Okay, but many people prefer $HOME | Yes — clearer & more portable |
Quick examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# This works (tilde expands) echo My home is ~ # This also works echo "My home is $HOME" # This DOES NOT expand tilde (because double quotes? Wait — actually tilde DOES expand in double quotes!) echo "My home is ~" # ← still expands to /home/webliance # But this does NOT echo 'My home is ~' # prints literally "My home is ~" |
4. Real-life examples you will use every day
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# Go home from anywhere cd ~ # or cd $HOME # Create folder in home mkdir ~/my_projects # Backup your .bashrc safely cp ~/.bashrc ~/backup_bashrc_$(date +%Y-%m-%d) # Script example (very common) #!/bin/bash BACKUP_FOLDER="$HOME/Backups" echo "Saving files to $BACKUP_FOLDER" tar -czf "$BACKUP_FOLDER/docs_$(date +%F).tar.gz" "$HOME/Documents" |
5. Small quiz for you (try in terminal!)
- What does this print? echo ~root
- What does this print? echo “~webliance”
- Which is better in a script that many people will use? ~/bin or $HOME/bin ?
(Answers: 1. /root 2. ~webliance (no expansion) 3. Usually $HOME/bin is safer)
Summary – like a good teacher would say
- ~ = quick nickname for your home (great when typing)
- $HOME = official name, always reliable (great in scripts + strings)
- Both take you to your personal room in the computer → /home/webliance (most likely)
- When in doubt in scripts → use $HOME — it’s boring but bulletproof 😄
Got it? Now open your terminal and play with these for 2 minutes:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
echo $HOME echo ~ cd ~ pwd mkdir ~/test_home ls ~ |
Tell me what you see — or ask next: “Teacher, explain ~/.bashrc” or “difference between / and ~” or anything!
Keep practicing — Bash will feel like home very soon! 🏠🐧
