为什么 zsh 获得 root 而 bash 没有?

Why does zsh get root while bash does not?

我有点困惑。如果我将 zsh 复制到 tmp 并更改 Set-UID 位 chmod u+s 更改用户,然后 运行 复制的 zsh。如果我 运行 whoami 我得到 root。如果我对 bash 执行相同的操作,在我 运行 bash 之后我会得到我之前登录的用户。他们有什么不同的原因吗?

这是 bash 的一个特意设置;要禁用它,运行 bash 使用 -p 选项。

更详细地说:当您 运行 一个 setuid 二进制文件时,进程的有效 uid (euid) 被设置为二进制文件的有效 uid,但它的真实 uid 没有改变。 bash 检测差异,并且(如果未通过 -p 选项)它将其 euid 重置为真实的 uid。

来自bash manual

Invoked with unequal effective and real UID/GIDs

If Bash is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied, no startup files are read, shell functions are not inherited from the environment, the SHELLOPTS, BASHOPTS, CDPATH, and GLOBIGNORE variables, if they appear in the environment, are ignored, and the effective user id is set to the real user id. If the -p option is supplied at invocation, the startup behavior is the same, but the effective user id is not reset.