`set -o posix` 在 WSL2 Ubuntu 20.04 上的 Bash 5.0.17 中不起作用 [英] `set -o posix` Not Working in Bash 5.0.17 on WSL2 Ubuntu 20.04

查看:158
本文介绍了`set -o posix` 在 WSL2 Ubuntu 20.04 上的 Bash 5.0.17 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所述,set -o posix 选项在 GNU/Linux bash 5.0.17 中似乎不起作用(我不确定它是否与 WSL2 Ubuntu 20.04 或任何东西,但要注意以防其他人在他们的机器上工作).

As mentioned in the title, the set -o posix option does not appear to be working in GNU/Linux bash 5.0.17 (I'm not sure it's specifically related to WSL2 Ubuntu 20.04 or anything, but noted it in case it is working for others on their machines).

我可以打开和关闭它:

$ set -o |grep posix
posix        off
$ set -o posix
$ set -o |grep posix
posix        on
$ set -o posix
$ set -o |grep posix
posix        off
$ set -o posix
$ set -o |grep posix
posix        on

但是,例如,当我能够执行以下操作时

but, for example, when on I am able to do the following

$ set -o posix
$ set -o |grep posix
posix        on
$ type cd
cd is a shell builtin
$ cd /
$ ls
bin   dev  home  lib    lib64   lost+found  mnt  proc  run   snap  sys  usr
boot  etc  init  lib32  libx32  media       opt  root  sbin  srv   tmp  var
$ cd() { :; }
$ type cd
cd is a function
cd ()
{
    :
}
$ cd ~
$ ls
bin   dev  home  lib    lib64   lost+found  mnt  proc  run   snap  sys  usr
boot  etc  init  lib32  libx32  media       opt  root  sbin  srv   tmp  var
$ unset -f cd
$ cd ~
$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

我能够覆盖所有特殊的内置函数(包括 builtintype!).另外,我不是 root,只是在我的用户帐户中.有谁知道我做错了什么?

I am able to override all special builtins (including builtin and type!). Also, I am not root, but just in my user account. Does anyone know what I am doing wrong?

更新:

@Shawn、@oguzismail 和@Dan 都给出了以下正确答案:cd 是一个常规 内置函数.我想补充一些说明,因为我认为这对像我这样的学习者很重要:

@Shawn, @oguzismail, and @Dan all give the correct answer below: cd is a regular builtin. I wanted to add some clarification as I think it will be important for learners like me:

@oguzismail 正确指向 Classic Shell Scripting 的部分 说明 cd 是一个常规的内置函数:

@oguzismail correctly points to the section of Classic Shell Scripting that explains that cd is a regular built-in:

为此,您可以编写一个名为 cd 的函数,shell 将首先找到你的函数,因为 cd 是一个常规的内置

For this reason, you can write a function named cd, and the shell will find your function first, since cd is a regular built-in

  • 第 7.9 节(Kindle 版第 261 页)
  • 我的困惑来自摘要中的陈述:

    My confusion came from the statement in the summary:

    内置命令的存在要么是因为它们改变了 shell 的内部状态并且必须是内置的(例如 cd),或者为了效率(例如测试).

    Built-in commands exist either because they change the shell’s internal state and must be built-in (such as cd), or for efficiency (such as test).

    • (第 7 节摘要,Kindle 版第 264 页)
    • 这里,cd 是一个内置函数,但正如作者之前所说,它是一个常规内置函数.它必须是一个内置于 shell 的命令,因为该命令改变了 shell 的内部状态(即它改变了 shell 的当前/当前工作目录),但它可以是 regular特殊 内置.正如@Dan 提到的,POSIX IEEE Std 1003.1-2017 标准,第 2.14 节 将其定义为 regular 内置函数.(至少有一个原因,Classic Shell Scripting 的第 7.9 节 表明这样做允许程序员自定义其行为.)

      Here, cd is a builtin, but as the author states previously, it is a regular builtin. It has to be a command built into the shell because the command changes the internal state of the shell (i.e. it changes the current/present working directory of the shell), but it can be either a regular or special builtin. As @Dan mentions, the POSIX IEEE Std 1003.1-2017 standard, Section 2.14 defines it as a regular builtin. (For at least one reason why, Section 7.9 of Classic Shell Scripting shows that doing so permits the programmer to customize its behavior.)

      请注意!(NB = Nota Bene):如果您确实选择用用户定义的函数覆盖 cd,请在要实际更改目录的函数定义中使用 command cd.函数 command 跳过在 特殊内置函数的 POSIX 定义的搜索顺序中查找函数 -->功能 -->常规内置插件 -->$PATH 上的外部命令.否则,如果您在用户定义的函数 cd 中使用 cd,您很可能会以无限递归循环结束.(经典 Shell 脚本 也涵盖了这一点.)

      Be Warned Though! (NB = Nota Bene): If you do choose to overwrite cd with a user-defined function, use command cd within the function definition where you want to actually change directories. The function command skips looking for functions in the POSIX-defined search order of special built-ins --> functions --> regular built-ins --> external commands on $PATH. Otherwise, if you use cd within your user-defined function cd, you will most likely end up with an infinite recursion loop. (Classic Shell Scripting also covers this point.)

      推荐答案

      set -o posix 工作正常:

        $ set -o posix
        $ set () { :; }
      bash: `set': is a special builtin
      

      cd 不是内置的 特殊,也不是 builtintype.

      cd is not a special built in, nor is builtin or type.

      特殊内置函数"列表在 posix 规范中明确定义.

      The list of "special builtins" is clearly defined in the posix specification.

      特殊内置"与任何其他命令不同,shell 可能会或可能不会将其作为内置命令实现.

      A "special built-in" is distinct from any other command which a shell may or may not implement as a built-in.

      这篇关于`set -o posix` 在 WSL2 Ubuntu 20.04 上的 Bash 5.0.17 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆