Meaning of tilde in Linux bash (not home directory

2019-01-16 05:52发布

First off, I know that ~/ is the home directory. CDing to ~ or ~/ takes me to the home directory.

However, cd ~X takes me to a special place, where X seems to be anything.

In bash, if I hit "cd ~" and hit tab, it shows a bunch of possible ~X options like ~mail and ~postgres and ~ssh. Going to those folders and doing a pwd shows me that these folders are not in the home directory; they're all over the place.

They are not aliases. I've checked. They're not env. variables, or else they'd require a $.

What is setting these links, and where can I find where these are being set?

8条回答
家丑人穷心不美
2楼-- · 2019-01-16 06:11

Are they the home directories of users in /etc/passwd? Services like postgres, sendmail, apache, etc., create system users that have home directories just like normal users.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-16 06:19

those are users, check your /etc/passwd

cd ~username

takes you to that users home dir

查看更多
狗以群分
4楼-- · 2019-01-16 06:21

Those are the home directories of the users. Try cd ~(your username), for example.

查看更多
劫难
5楼-- · 2019-01-16 06:22

It's possible you're seeing OpenDirectory/ActiveDirectory/LDAP users "automounted" into your home directory.

In *nix, ~ will resolve to your home directory. Likewise ~X will resolve to 'user X'.

Similar to automount for directories, OpenDirectory/ActiveDirectory/LDAP is used in larger/corporate environments to automount user directories. These users may be actual people or they can be machine accounts created to provide various features.

If you type ~Tab you'll see a list of the users on your machine.

查看更多
家丑人穷心不美
6楼-- · 2019-01-16 06:26

It's a Bash feature called "tilde expansion". It's a function of the shell, not the OS. You'll get different behavior with csh, for example.

To answer your question about where the information comes from: your home directory comes from the variable $HOME (no matter what you store there), while other user's homes are retrieved real-time using getpwent(). This function is usually controlled by NSS; so by default values are pulled out of /etc/passwd, though it can be configured to retrieve the information using any source desired, such as NIS, LDAP or an SQL database.

Tilde expansion is more than home directory lookup. Here's a summary:

~              $HOME
~fred          (freds home dir)

~+             $PWD   (same effect as ./)
~-             $OLDPWD (your previous directory)
~1             `dirs +1`
~2             `dirs +2`
~-1            `dirs -1`

dirs and ~1, ~-1, etc., are used in conjunction with pushd and popd.

查看更多
Emotional °昔
7楼-- · 2019-01-16 06:26

If you're using autofs then the expansion might actually be coming from /etc/auto.home (or similar for your distro). For example, my /etc/auto.master looks like:

/home2 auto.home --timeout 60

and /etc/auto.home looks like:

mgalgs -rw,noquota,intr space:/space/mgalgs
查看更多
登录 后发表回答