📖 Scenario: You are writing a simple bash script to work with files in your home directory. Instead of typing the full path, you want to use the tilde (~) symbol to represent your home folder.
🎯 Goal: Learn how to use tilde expansion (~) in bash scripts to refer to the home directory easily.
📋 What You'll Learn
Create a variable with a path using tilde (~)
Create a variable with a filename
Combine the path and filename using tilde expansion
Print the full path using tilde expansion
💡 Why This Matters
🌍 Real World
Using tilde expansion helps you write shorter and clearer bash scripts that work with files in your home directory without typing the full path.
💼 Career
Many automation and scripting tasks require working with user files. Knowing tilde expansion saves time and avoids errors when specifying paths.
Progress0 / 4 steps
1
Create a variable with the home directory path using tilde (~)
Create a variable called home_path and set it to ~/documents without surrounding quotes.
Bash Scripting
Hint
Assign without quotes: home_path=~/documents. This allows the shell to expand the tilde immediately.
2
Create a variable for the filename
Create a variable called file_name and set it to the string notes.txt exactly.
Bash Scripting
Hint
Just assign the string notes.txt to file_name without quotes.
3
Combine the path and filename using tilde expansion
Create a variable called full_path that combines home_path and file_name with a slash / between them, using double quotes to allow variable expansion.
Bash Scripting
Hint
Use double quotes around $home_path/$file_name so the shell expands the variables.
4
Print the full expanded path
Use the echo command to print the value of full_path so the tilde expands to your home directory.
Bash Scripting
Hint
Just write echo $full_path to see the expanded path.
Practice
(1/5)
1. What does the tilde symbol ~ represent in a bash shell?
easy
A. The current user's home directory
B. The root directory
C. The current working directory
D. A temporary directory
Solution
Step 1: Understand tilde usage in bash
The tilde ~ is a shortcut that expands to the current user's home directory path.
Step 2: Compare options with definition
Only the current user's home directory correctly states that ~ means the current user's home directory.
Final Answer:
The current user's home directory -> Option A
Quick Check:
Tilde (~) = home directory [OK]
Hint: Remember: ~ always means your home folder [OK]
Common Mistakes:
Confusing ~ with root directory /
Thinking ~ means current directory
Assuming ~ is a temporary folder
2. Which of the following is the correct way to refer to another user's home directory named alice using tilde expansion?
easy
A. ~ /alice
B. ~/alice
C. ~ alice
D. ~alice
Solution
Step 1: Understand tilde with username
Using ~username refers to that user's home directory without a slash after the username.
Step 2: Check each option
~ /alice has a space after ~, invalid. ~/alice refers to alice subfolder in current user's home. ~alice correctly refers to alice's home directory. ~ alice has space between ~ and alice, invalid syntax.
Final Answer:
~alice -> Option D
Quick Check:
~username = user's home directory [OK]
Hint: Use ~username without spaces or slashes to get their home [OK]
Common Mistakes:
Adding a slash immediately after username
Putting space between ~ and username
Using ~/username which means subfolder in current user's home
3. What will be the output of the following bash command if the current user's home directory is /home/bob?
echo ~
medium
A. /home/bob
B. ~
C. /root
D. Error: command not found
Solution
Step 1: Understand echo with tilde
The shell expands ~ to the current user's home directory before running echo.
Step 2: Apply expansion
Since the home directory is /home/bob, the command becomes echo /home/bob, which prints /home/bob.
Final Answer:
/home/bob -> Option A
Quick Check:
echo ~ outputs home directory path [OK]
Hint: echo ~ prints your home folder path [OK]
Common Mistakes:
Expecting echo to print ~ literally
Confusing with root directory /root
Thinking echo ~ causes error
4. You wrote this bash command to list files in your home directory:
ls ~
But it shows an error: ls: cannot access '~': No such file or directory. What is the most likely cause?
medium
A. ls command does not support ~
B. You put single quotes around ~, preventing expansion
C. You need to use $HOME instead of ~
D. Your home directory does not exist
Solution
Step 1: Understand tilde expansion and quoting
Tilde expansion happens only when ~ is unquoted. Quoting it (like '~') disables expansion.
Step 2: Analyze error cause
The error shows ls tried to find a folder literally named '~', meaning expansion did not happen due to quotes.
Final Answer:
You put single quotes around ~, preventing expansion -> Option B
Quick Check:
Quotes block tilde expansion [OK]
Hint: Never quote ~ if you want it expanded [OK]
Common Mistakes:
Assuming ~ always expands even in quotes
Thinking home directory is missing
Believing ls cannot use ~
5. You want to write a bash script that copies a file named notes.txt from the home directory of user carol to your current directory. Which command correctly uses tilde expansion to do this?
hard
A. cp ~/carol/notes.txt .
B. cp ~ /carol/notes.txt .
C. cp ~carol/notes.txt .
D. cp ~carol notes.txt .
Solution
Step 1: Understand tilde with username and path
~carol expands to carol's home directory. Adding /notes.txt accesses the file inside her home.
Step 2: Check each option
cp ~carol/notes.txt . correctly uses ~carol/notes.txt. cp ~/carol/notes.txt . uses ~/carol/notes.txt which means a subfolder carol inside your home, wrong user. cp ~ /carol/notes.txt . has a space breaking the path. cp ~carol notes.txt . misses the slash before filename, so it tries to copy a directory.
Final Answer:
cp ~carol/notes.txt . -> Option C
Quick Check:
~username/file = file in that user's home [OK]
Hint: Use ~username/path to access files in another user's home [OK]
Common Mistakes:
Using ~/username which is wrong path
Adding spaces breaking tilde expansion
Forgetting slash between username and file
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.