Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Tilde expansion (~) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Tilde expansion (~)
Start command with ~
↓
Detect ~ at start
↓
Replace ~ with home directory path
↓
Execute command with expanded path
↓
End
The shell detects a tilde (~) at the start of a path and replaces it with the user's home directory before running the command.
Execution Sample
Bash Scripting
echo ~
echo ~/Documents
cd ~
pwd
This script shows how ~ expands to the home directory in different commands.
Execution Table
StepCommandTilde DetectedExpansion ResultOutput/Effect
1echo ~Yes/home/username/home/username
2echo ~/DocumentsYes/home/username/Documents/home/username/Documents
3cd ~Yes/home/usernameChanges directory to /home/username
4pwdNoN/A/home/username
5EndN/AN/AScript ends
💡 All commands executed; tilde expanded to /home/username where applicable.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
PWD/current/directory/current/directory/current/directory/home/username/home/username/home/username
Key Moments - 2 Insights
Why does ~ expand only when it is at the start of the path?
Because tilde expansion in bash only happens when ~ is the first character of the word, as shown in steps 1-3 where ~ is at the start and expands, but not in step 4 where pwd has no ~.
What happens if ~ is used in the middle of a path?
It does not expand. For example, echo /tmp/~ would print /tmp/~ literally, because expansion only occurs at the start, as seen in the execution_table where only leading ~ expands.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of 'echo ~' at step 1?
A/home/username
B~
C/tmp
DError
💡 Hint
Check the 'Output/Effect' column for step 1 in the execution_table.
At which step does the current directory change to the home directory?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Output/Effect' and 'PWD' variable changes in variable_tracker after each step.
If the command was 'echo /tmp/~', what would be the output?
A/tmp/home/username
B/home/username/tmp/~
C/tmp/~
DError
💡 Hint
Recall that tilde expands only at the start of the path, as shown in the execution_table steps.
Concept Snapshot
Tilde (~) expansion replaces ~ at the start of a path with the user's home directory.
Used in commands like cd, echo, and file paths.
Only expands if ~ is the first character of the word.
Example: ~ -> /home/username, ~/docs -> /home/username/docs.
No expansion if ~ is in the middle or end of a path.
Full Transcript
Tilde expansion in bash means the shell looks for a tilde character at the start of a path and replaces it with the user's home directory path. For example, when you run 'echo ~', the shell changes ~ to '/home/username' and prints it. Similarly, 'cd ~' changes the current directory to the home directory. This expansion only happens if the tilde is the first character in the path. If the tilde appears later in the path, it is treated as a normal character and not expanded. This behavior helps users quickly refer to their home directory in commands.

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

  1. Step 1: Understand tilde usage in bash

    The tilde ~ is a shortcut that expands to the current user's home directory path.
  2. Step 2: Compare options with definition

    Only the current user's home directory correctly states that ~ means the current user's home directory.
  3. Final Answer:

    The current user's home directory -> Option A
  4. 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

  1. Step 1: Understand tilde with username

    Using ~username refers to that user's home directory without a slash after the username.
  2. 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.
  3. Final Answer:

    ~alice -> Option D
  4. 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

  1. Step 1: Understand echo with tilde

    The shell expands ~ to the current user's home directory before running echo.
  2. Step 2: Apply expansion

    Since the home directory is /home/bob, the command becomes echo /home/bob, which prints /home/bob.
  3. Final Answer:

    /home/bob -> Option A
  4. 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

  1. Step 1: Understand tilde expansion and quoting

    Tilde expansion happens only when ~ is unquoted. Quoting it (like '~') disables expansion.
  2. Step 2: Analyze error cause

    The error shows ls tried to find a folder literally named '~', meaning expansion did not happen due to quotes.
  3. Final Answer:

    You put single quotes around ~, preventing expansion -> Option B
  4. 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

  1. Step 1: Understand tilde with username and path

    ~carol expands to carol's home directory. Adding /notes.txt accesses the file inside her home.
  2. 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.
  3. Final Answer:

    cp ~carol/notes.txt . -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard