Tilde expansion is a feature in Bash that allows users to use the tilde character (~) as equivalent to a certain path. This is particularly useful for quickly navigating to user’s home directory or directories relative to the home directory. Moreover, one user can switch to another user’s home directory using this tilde expansion. This article is intended to clear your idea about tilde expansion in Bash.
Key Takeaways
- Expanding HOME variable using tilde expansion.
- Changing to last visited directory using tilde.
- Accessing the directory stack using tilde expansion.
Free Downloads
What is Tilde Expansion?
The tilde(~) character is typically located in the top-left corner of the keyboard, to the left of the number “1” key and above the “Tab” key. It has a special meaning to Bash shell. It’s a shorthand notation for representing user’s home directory. This shortcut is quite useful when changing directories from the command line.
4 Different Cases of Tilde Expansion in Bash
Tilde expansion is mainly used for changing directory to home directory. However, this expansion has other usage also. One can go to other user’s home directory, access directory from the directory stack and go back to previous working directory using tilde expansion.
Case 1: Expanding HOME Variable and User’s Home Directory Using Tilde in Bash
In Bash, the tilde (~) symbol takes users to their home directory, which is determined by the HOME environment variable.
Open your terminal and run the command below:
echo ~

echo $HOME

export HOME=/usr
The new value of the HOME variable is set as “/usr”.
cd ~
pwd

unset HOME
echo $HOME
echo ~
cd ~

Creating Files in a Directory Using Tilde Expansion
As previously mentioned, it’s evident that tilde expansion allows one to navigate relative to their home directory. Utilizing tilde expansion makes the process of generating files within a specific directory quite straightforward. Let’s visualize the idea in a script.
Steps to Follow >
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file named createfiles.sh in the build-in nano editor:
nano bexpansion.sh
- nano: Opens a file in the Nano text editor.
- createfiles.sh: Name of the file.
❸ Copy the following scripts and paste them into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script in a text editor and save it as .sh file.
Script (createfiles.sh) >
#!/bin/bash
# Define the path to the "January" directory using tilde expansion
january_directory=~/January
# Check if the "January" directory exists
if [ ! -d "$january_directory" ]; then
# If it doesn't exist, create it
mkdir -p "$january_directory"
fi
# Create the "record.txt" file inside the "January" directory
touch "$january_directory/record.txt"
echo "File 'record.txt' created in $january_directory."
“#!/bin/bash” specifies that the script should be interpreted and run using the Bash shell.
Then it creates a file named “record.txt” within a directory named “January” located in the user’s home directory. The script checks if the “January” directory exists; if not, it creates it using mkdir command. Following this, the script utilizes the touch command to create the “record.txt” file within the “January” directory. Finally, it provides feedback to the user by printing a message confirming the successful creation of “record.txt” in the specified directory.
❹ Use the following two commands to make the file executable:
chmod u+x createfiles.sh
- chmod: Changes permissions.
- u+x: Giving the owner executing permission.
- createfiles.sh: Name of the script.
❺ Run the script by the following command:
./createfiles.sh

Case 2: Expanding Current and Previous Working Directory
Using tilde sign, one can expand the current or the previous working directory. One needs to put a plus(+) or minus(–) after tilde sign to expand current and previous working directory respectively. It works no matter the HOME variable is set or not.
For instance, currently I am in my home directory which is “/home/laku”. Let’s go to the “/usr” directory for example. Now, run the following command:
echo ~+

However, if you place a minus after the tilde sign, it will take you to the previous working directory.
cd ~-

Case 3: Directly Expand Other User’s Home Directory Using Tilde Expansion in Bash
One can directly go to another user’s home directory by adding name of the user after the tilde sign. Let’s first create a user named “kelly” using the adduser command.

cd ~kelly

Case 4: Accessing Directory from Directory Stack Using Tilde Expansion in Bash
Bash has a directory stack that can be accessed using the dirs command. You can push a directory in the directory stack using the pushd command. Otherwise, it is filled with the latest visited directory only. Importantly, one can access the items of directory stack using tilde expansion. Let me push some directories in the directory stack and visualize it using dirs command.
pushd /home/laku/Desktop/
pushd /home/laku/Downloads/
pushd /home/laku/Documents/
dirs

First of all, to get the first item from the directory stack run the following command.
echo ~0
Furthermore, to get the third item from the stack put 2 after the tilde sign.
echo ~2

Conclusion
In conclusion, there are different uses of Bash tilde expansion. All of these are related to changing directories quickly and efficiently. I believe from now you can employ those techniques to move around directories.
People Also Ask
Related Articles
- How to Use Brace Expansion in Bash Scripting [3 Cases]
- Parameter Expansion in Bash [3 Main Types]
- Arithmetic Expansion in Bash [3 Practical Applications]
- An Exhaustive Guide to Bash History Expansion
- What is Array Expansion in Bash [4 Useful Applications]
- Glob Expansion in Bash
<< Go Back to An Overview of Shell Expansion in Bash | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners




