0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 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.