`export PS1='value'` 不会传播到 root 的 (Korn) 子 shell?

发布于 2024-10-10 05:12:52 字数 273 浏览 5 评论 0原文

请考虑以下 /root/.profile:

export PS1=value1
export x=value2

为什么登录 shell 显示预期的提示(并且 $x 作为 value2),而子 shell 一直将 $x 显示为 value2 而 $PS1 显示为“#”?以防万一,我正在 OpenBSD 下尝试这个。

[是的,我知道...如果我不知道这一点,我到底在用 OpenBSD 做什么?只是在一个孤立的、绝对非生产的虚拟机中玩耍......

Please consider the following /root/.profile:

export PS1=value1
export x=value2

How come the login shell shows the expected prompt (and $x as value2), while the subshells keep showing $x as value2 but $PS1 as '#'? Just in case, I'm trying this under OpenBSD.

[Yeah, I know... What on earth am I doing with OpenBSD if I don't know this? Just toying... in an isolated, most definitely non-production VM =).]

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

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

发布评论

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

评论(3

哀由 2024-10-17 05:12:52

因为你的子 shell 正在获取重置 PS1 的东西。

要调试此问题,请尝试 ksh -x。如果这没有帮助,您可以尝试在 strace< 中运行 ksh /code> 或等效的系统调用跟踪工具。

Because your subshells are sourcing something that's resetting PS1.

To debug this, try ksh -x. If that doesn't help, you could try running ksh in strace or an equivalent system call tracing tool.

池予 2024-10-17 05:12:52

如果有人感兴趣,终于有人在 OpenBSD 邮件列表中解释了这一点< /a>.这种行为实际上是 OpenBSD 中所期望的,正如 /usr/src/bin/ksh/main.c

safe_prompt = ksheuid ? "$ " : "# ";
{
    struct tbl *vp = global("PS1");

    /* Set PS1 if it isn't set, or we are root and prompt doesn't
     * contain a # or \$ (only in ksh mode).
     */
    if (!(vp->flag & ISSET) ||
        (!ksheuid && !strchr(str_val(vp), '#') &&
        (Flag(FSH) || !strstr(str_val(vp), "\\$"))))
        /* setstr can't fail here */
        setstr(vp, safe_prompt, KSH_RETURN_ERROR);
}

In case anybody is interested, somebody finally explained this in an OpenBSD mailing list. This behavior is actually expected in OpenBSD, as explained in a comment inside /usr/src/bin/ksh/main.c:

safe_prompt = ksheuid ? "$ " : "# ";
{
    struct tbl *vp = global("PS1");

    /* Set PS1 if it isn't set, or we are root and prompt doesn't
     * contain a # or \$ (only in ksh mode).
     */
    if (!(vp->flag & ISSET) ||
        (!ksheuid && !strchr(str_val(vp), '#') &&
        (Flag(FSH) || !strstr(str_val(vp), "\\$"))))
        /* setstr can't fail here */
        setstr(vp, safe_prompt, KSH_RETURN_ERROR);
}
妖妓 2024-10-17 05:12:52

检查以下文件是否存在恶意 PS1 分配。

   /etc/profile
          The  system  wide initialization file, executed for login shells.
   $HOME/.profile
          The personal initialization  file,  executed  for  login  shells
          after /etc/profile.
   $HOME/..kshrc
          Default  personal  initialization file, executed for interactive
          shells when ENV is not set.
   /etc/suid_profile
          Alternative initialization file, executed when instead  of  per-
          sonal  initialization  file  when the real and effective user or
          group id do not match.

该手册页中可能有一个拼写错误,它的意思可能是:$HOME/.kshrc

Check the following files for rogue PS1 assignments.

   /etc/profile
          The  system  wide initialization file, executed for login shells.
   $HOME/.profile
          The personal initialization  file,  executed  for  login  shells
          after /etc/profile.
   $HOME/..kshrc
          Default  personal  initialization file, executed for interactive
          shells when ENV is not set.
   /etc/suid_profile
          Alternative initialization file, executed when instead  of  per-
          sonal  initialization  file  when the real and effective user or
          group id do not match.

There may be a typo in that manpage, it probably meant to say: $HOME/.kshrc

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