0
0
Bash Scriptingscripting~5 mins

Tilde expansion (~) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the tilde (~) symbol represent in bash scripting?
The tilde (~) represents the current user's home directory path. For example, ~ expands to /home/username.
Click to reveal answer
beginner
How does bash handle the tilde (~) when used at the start of a path?
Bash replaces the tilde (~) at the start of a path with the full path of the user's home directory before running the command.
Click to reveal answer
intermediate
What happens if you use ~username in bash?
Using ~username expands to the home directory of the specified user, not the current user. For example, ~john expands to /home/john.
Click to reveal answer
intermediate
Does tilde expansion happen if ~ is used in the middle of a path, like /tmp/~/file?
No, tilde expansion only happens if ~ is at the start of the path or immediately after a colon (:). In /tmp/~/file, ~ is treated as a normal folder name.
Click to reveal answer
intermediate
Can tilde expansion be used inside quotes in bash?
Tilde expansion does NOT happen inside single quotes ('~'), and it does NOT happen inside double quotes ("~").
Click to reveal answer
What does the command cd ~ do in bash?
AChanges directory to the current user's home directory
BChanges directory to the root directory
CChanges directory to a folder named ~ in the current directory
DThrows an error
What will echo ~john output if user 'john' exists?
AThe current user's home directory
BThe string '~john' literally
CThe home directory path of user 'john'
DAn error message
Which of these paths will NOT trigger tilde expansion?
A/tmp/~/file
B~/documents
C:~/.bashrc
D~
Does tilde expansion occur inside single quotes in bash?
AYes, always
BNo, never
COnly if the quotes are empty
DOnly if the tilde is at the end
What does the command ls ~ do?
ALists files in the current directory
BLists files in a folder named ~
CLists files in the root directory
DLists files in the user's home directory
Explain what tilde expansion (~) is and how it helps when working with file paths in bash.
Think about how ~ saves you from typing full home directory paths.
You got /4 concepts.
    Describe when tilde expansion does NOT happen in bash and why that matters.
    Consider how quoting and position affect tilde behavior.
    You got /4 concepts.