0
0
Bash Scriptingscripting~15 mins

Tilde expansion (~) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Tilde Expansion (~) in Bash Scripts
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Just write echo $full_path to see the expanded path.