Tilde Expansion
(this post if part of the material I cover in my devops course)
About tilde expansion
This post is based on the bash tilde expansion documentation.
- Let's say that you enter several letters, and them the ~ (tilde) character.
- If non of the letters are quoted, then bash will try to treat these letters as a login name.
It will then try to replace the tilde expression with the home directory of that user. - Example:
1$> printf "%s\n" ~dave
2/home/dave
3$> printf "%s\n" ~yuval
4/home/yuval
5$>
- Without a prefix, the tilde expansion will expand to the current user home directory:
1$> whoami
2osboxes
3$> echo ~
4/home/osboxes
5$>
The tilde expansion and the directory stack
- The ~+ will expand to the current working directory:
1$> echo ~
2/home/osboxes
3$> pwd
4/home/osboxes/Documents/library
5$> echo ~+
6/home/osboxes/Documents/library
7$>
- A complex example:
1$> pwd
2/home/osboxes
3$> pushd Documents/
4~/Documents ~
5$> pushd library/
6~/Documents/library ~/Documents ~
7$> pushd one
8~/Documents/library/one ~/Documents/library ~/Documents ~
9$>
10$> echo ~-3
11/home/osboxes/Documents/library/one
12$> echo ~-2
13/home/osboxes/Documents/library
14$> echo ~-1
15/home/osboxes/Documents
16$> echo ~-0
17/home/osboxes
18$> echo ~
19/home/osboxes
20$>