Ale*_*lls 11 shell bash bash-functions
当我打开终端会话时,我得到了这个:
sh:为“read.json”导入函数定义时出错
sh:为“ts-project”导入函数定义时出错
sh 不喜欢这些函数,因为它们看起来像:
read.json(){
::
}
Run Code Online (Sandbox Code Playgroud)
和
ts-project(){
::
}
Run Code Online (Sandbox Code Playgroud)
真正的问题是 - 为什么要sh触摸/解释这些文件?我在 MacOS 上看到过这个,这真是个谜。我认为只有 bash 会加载这些文件。
更新: bash 和 sh 没什么特别的。当我在终端中输入 bash 时,我得到了这个:
alex$ bash
beginning to load .bashrc
finished loading .bashrc
bash-3.2$
Run Code Online (Sandbox Code Playgroud)
当我sh在终端中输入时,我得到以下信息:
alex$ sh
sh: error importing function definition for `read.json'
sh: error importing function definition for `ts-project'
sh-3.2$
Run Code Online (Sandbox Code Playgroud)
mos*_*svy 20
当bash伪装成 POSIX shell 试图从环境中导入这些函数时,会发生该错误,而不是通过解释诸如此类的文件来加载它们时~/.bashrc。简化示例:
foo.bar(){ true; }; export -f foo.bar; bash --posix -c true
bash: error importing function definition for `foo.bar'
Run Code Online (Sandbox Code Playgroud)
我原以为bash在 posix 模式下不会从环境中加载函数,但它确实如此,并且只有在它们的名称包含有趣的字符时才会抱怨。
请注意,bash当设置POSIXLY_CORRECTorPOSIX_PEDANTIC环境变量或使用--enable-strict-posix-default/编译时,它也将在 posix 模式下运行STRICT_POSIX。
后者似乎是/bin/sh在 MacOS 上的情况(在此处查找PRODUCT_NAME = sh),我希望在使用popen(3)或等库函数时也会触发此错误system(3)。