This article we will learn about the tilde expansion in Linux. There will be many times, you will encounter many examples using tilde in Linux commands.
So what is tilde expansion?
Tilde expansion
There will be times when you see commands like this.
cd ~
Or like this.
source ~/.bashrc
Have you ever wondered what that tilde is? What does it mean in the Linux command?
That extended tilde has a special meaning in Linux. In Linux commands, if it starts with a tilde, it has an extended meaning for the user’s home directory.
Going back to some examples, the simplest one is to try echo
.
danie@linuxmint ~ $ echo ~
/home/danie
Here, my user is danie
and my home directory is /home/danie
. So, when I type echo ~
it will print my home directory.
Recommended Reading: What is the expansion in Linux?
What if the user does not have a name? This can be seen when switching from a normal user to a user root in Linux Mint.
The tilde will expand to the current user’s home directory, here is my normal user.
You see in the image above, I have switched to user root. When echo ~
it prints the home directory of the current user (ie the user I use to login to the computer).
Conclusion
The article was quite clear example of tilde expansion. It is not difficult but if you do not understand its meaning, sometimes you will not understand commands from others’ instructions.
(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).