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

发布于 2024-07-24 05:06:13 字数 478 浏览 7 评论 0原文

首先,我知道 ~/ 是主目录。 CD 到 ~~/ 将我带到主目录。

然而,cd ~X 将我带到了一个特殊的地方,其中 X 似乎是任何东西。

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

终止放荡 2024-07-31 05:06:13

这是一个名为“波形符扩展”的 Bash 功能。 这是 shell 的功能,而不是操作系统的功能。 例如,使用 csh 时您会得到不同的行为。

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

波形符扩展不仅仅是主目录查找。 这里总结一下:

~              $HOME
~fred          (freds home dir)

~+             $PWD       (your current working directory)
~-             $OLDPWD    (your previous directory)
~1             `dirs +1`
~2             `dirs +2`
~-1            `dirs -1`

dirs~1~-1等,与pushd配合使用和 popd。

编辑添加:

正如Sean Bright在评论中指出的那样,有关主目录的基线波形符行为是已编入POSIX 兼容 shell 的标准行为。 另外,还指定了 wordexp() C API 函数来实现这种行为。 但显然,请谨慎使用。

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       (your current working directory)
~-             $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.

Edited to add:

As Sean Bright pointed out in a comment, the baseline tilde behavior regarding home directories is codified as standard behavior for POSIX-compliant shells. Additionally, the wordexp() C API function is specified to implement this behavior. Though, obviously, use with caution.

苦笑流年记忆 2024-07-31 05:06:13

这些是用户的主目录。 例如,尝试 cd ~(您的用户名)

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

蓝眸 2024-07-31 05:06:13

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

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.

丶情人眼里出诗心の 2024-07-31 05:06:13

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

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

Those are users. Check your /etc/passwd.

cd ~username takes you to that user's home directory.

束缚m 2024-07-31 05:06:13

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

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

首先注意环境变量 $HOME 的值; 我特意将 $HOME 设置为本地文件系统,而不是 NFS 安装的文件系统。 第二个从密码文件中读取(大约;NIS 让事情变得有点复杂)并发现密码文件显示我的主目录是 /u/jleffler 并更改为该目录。

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

将其应用于问题 - 正如其他人指出的那样,“cd ~x”会转到用户“x”的主目录,更一般地,每当完成波浪号扩展时,~x 表示用户“x”的主目录(如果用户“x”不存在则出错)。


可能值得一提的是:

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

我无法立即找到“~+”的用途,除非您在通向当前目录的路径中移动符号链接来执行一些奇怪的操作。

您还可以这样做:

cd -

~- 含义相同。

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 $HOME to 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/jleffler and changes to that directory.

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.

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, ~x means the home directory of user 'x' (and it is an error if user 'x' does not exist).


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 ~-.

桜花祭 2024-07-31 05:06:13

如果您使用的是 autofs,那么扩展实际上可能来自 /etc/auto.home (或您的发行版的类似内容)。 例如,我的 /etc/auto.master 看起来像:

/home2 auto.home --timeout 60

/etc/auto.home 看起来像:

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

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
风苍溪 2024-07-31 05:06:13

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

在 *nix 中,~ 将解析为您的主目录。 同样,~X 将解析为“用户 X”。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文