Challenge - 5 Problems
Tilde Expansion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:30remaining
What is the output of this command?
Consider the following bash command run by user 'alice' whose home directory is '/home/alice':
What will be printed?
echo ~What will be printed?
Bash Scripting
echo ~
Attempts:
2 left
💡 Hint
The tilde (~) expands to the current user's home directory in bash.
✗ Incorrect
In bash, the tilde (~) alone expands to the home directory of the current user. Since the user is 'alice', it expands to '/home/alice'.
💻 Command Output
intermediate1:30remaining
What does this command output?
Given that user 'bob' exists with home directory '/home/bob', what is the output of:
when run by user 'alice'?
echo ~bobwhen run by user 'alice'?
Bash Scripting
echo ~bob
Attempts:
2 left
💡 Hint
Tilde followed by a username expands to that user's home directory.
✗ Incorrect
In bash, ~username expands to the home directory of that user. Since 'bob' exists with home '/home/bob', it expands accordingly.
📝 Syntax
advanced2:00remaining
Which command correctly uses tilde expansion to list files in the home directory?
You want to list all files in your home directory using tilde expansion. Which of the following commands will work correctly?
Attempts:
2 left
💡 Hint
Tilde expansion does not occur inside quotes.
✗ Incorrect
Tilde expansion happens only when the tilde is unquoted. Option C uses unquoted ~/, so it expands correctly. Options B and C quote the tilde, so no expansion occurs. Option C works but lists the home directory without trailing slash, which is valid but less explicit.
💻 Command Output
advanced1:30remaining
What is the output of this command?
Assuming user 'carol' has home directory '/home/carol', what is the output of:
when run by user 'dave'?
echo ~carol/Documentswhen run by user 'dave'?
Bash Scripting
echo ~carol/Documents
Attempts:
2 left
💡 Hint
Tilde expansion works with usernames followed by paths.
✗ Incorrect
The tilde with username expands to that user's home directory, and the rest of the path is appended. So it becomes '/home/carol/Documents'.
💻 Command Output
expert2:00remaining
What is the output of this script snippet?
Consider this bash script snippet run by user 'eve' whose home directory is '/home/eve':
What will be printed?
dir=~ echo "$dir"
What will be printed?
Bash Scripting
dir=~
echo "$dir"Attempts:
2 left
💡 Hint
Tilde expansion does not happen during variable assignment without evaluation.
✗ Incorrect
When assigning dir=~, the tilde is not expanded because tilde expansion happens only in unquoted command arguments, not in variable assignments. So dir contains the literal '~'.