Linux bash 中波浪号的含义(不是主目录)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/998626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:25:30  来源:igfitidea点击:

Meaning of tilde in Linux bash (not home directory)

linuxbashhome-directory

提问by jbu

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

首先,我知道那~/是主目录。CDing 到~~/带我到主目录。

However, cd ~Xtakes me to a special place, where Xseems to be anything.

然而,cd ~X把我带到了一个特殊的地方,那里X似乎什么都没有。

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

在 bash 中,如果我点击“ cd ~”并点击 Tab,它会显示一堆可能的~X选项,比如~mailand~postgres~ssh。转到这些文件夹并执行 apwd显示这些文件夹不在主目录中;他们到处都是。

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

它们不是别名。我查过了。它们不是env.变量,否则它们需要一个$.

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

什么是设置这些链接,我在哪里可以找到这些链接的设置位置?

回答by Sean Bright

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.

他们是用户的主目录/etc/passwd吗?postgres、sendmail、apache 等服务会像普通用户一样创建具有主目录的系统用户。

回答by Ana Betts

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

这些是用户的主目录。试试cd ~(your username),例如。

回答by Joakim Elofsson

Those are users. Check your /etc/passwd.

那些是用户。检查您的/etc/passwd.

cd ~usernametakes you to that user's home directory.

cd ~username将您带到该用户的主目录。

回答by Jonathan Leffler

On my machine, because of the way I have things set up, doing:

在我的机器上,由于我设置的方式,执行以下操作:

cd ~             # /work1/jleffler
cd ~jleffler     # /u/jleffler

The first pays attention to the value of environment variable $HOME; I deliberately set my $HOMEto a local file system instead of an NFS-mounted file system. The second reads from the password file (approximately; NIS complicates things a bit) and finds that the password file says my home directory is /u/jlefflerand changes to that directory.

第一个注意环境变量的值$HOME;我特意将 my 设置$HOME为本地文件系统而不是 NFS 挂载的文件系统。第二个从密码文件中读取(大约;NIS 使事情复杂化了一点)并发现密码文件说我的主目录是/u/jleffler并更改为该目录。

The annoying stuff is that most software behaves as above (and the POSIX specification for the shell requires this behaviour). I use some software (and I don't have much choice about using it) that treats the information from the password file as the current value of $HOME, which is wrong.

令人讨厌的是,大多数软件的行为都如上(并且 shell 的 POSIX 规范要求这种行为)。我使用了一些软件(我没有太多使用它的选择)将密码文件中的信息视为 $HOME 的当前值,这是错误的。

Applying this to the question - as others have pointed out, 'cd ~x' goes to the home directory of user 'x', and more generally, whenever tilde expansion is done, ~xmeans the home directory of user 'x' (and it is an error if user 'x' does not exist).

将此应用于问题 - 正如其他人指出的那样,“ cd ~x”转到用户“x”的主目录,更一般地说,每当完成波浪号扩展时,都~x意味着用户“x”的主目录(这是一个错误如果用户“x”不存在)。



It might be worth mentioning that:

可能值得一提的是:

cd ~-       # Change to previous directory ($OLDPWD)
cd ~+       # Change to current directory ($PWD)

I can't immediately find a use for '~+', unless you do some weird stuff with moving symlinks in the path leading to the current directory.

我无法立即找到 ' ~+'的用途,除非您在通向当前目录的路径中移动符号链接来做一些奇怪的事情。

You can also do:

你也可以这样做:

cd -

That means the same as ~-.

这意味着与~-.

回答by tylerl

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.

这是一个名为“波浪号扩展”的 Bash 功能。这是外壳的功能,而不是操作系统。例如,使用 csh 会得到不同的行为。

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.

回答有关信息来自何处的问题:您的主目录来自变量$HOME(无论您在那里存储什么),而其他用户的主目录使用getpwent(). 该功能通常由NSS控制;因此,默认情况下会从 中提取值/etc/passwd,但可以将其配置为使用所需的任何来源(例如 NIS、LDAP 或 SQL 数据库)检索信息。

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`

dirsand ~1, ~-1, etc., are used in conjunction with pushdand popd.

dirs~1~-1等与pushd和结合使用popd

回答by mgalgs

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

如果您正在使用,autofs那么扩展实际上可能来自/etc/auto.home(或类似于您的发行版)。例如,我的/etc/auto.master样子:

/home2 auto.home --timeout 60

and /etc/auto.homelooks like:

/etc/auto.home看起来像:

mgalgs -rw,noquota,intr space:/space/mgalgs

回答by JS.

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

您可能会看到 OpenDirectory/ActiveDirectory/LDAP 用户“自动挂载”到您的主目录中。

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

在 *nix 中,~将解析到您的主目录。同样~X将解析为“用户 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.

与目录的自动挂载类似,OpenDirectory/ActiveDirectory/LDAP 用于大型/公司环境以自动挂载用户目录。这些用户可能是真实的人,也可能是为提供各种功能而创建的机器帐户。

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

如果您键入,~Tab您将看到您机器上的用户列表。