函数名在bash中有效但在sh中没有

use*_*000 9 bash shell sh

在玩bash和sh时,我发现以下内容在bash中有效:

system.out.println () { printf "$1"; }
Run Code Online (Sandbox Code Playgroud)

但不是sh:

sh: `system.out.println': not a valid identifier
Run Code Online (Sandbox Code Playgroud)

为什么会出现这种差异?上面定义的函数是否违反导致此错误的某些约定(POSIX等)?

Noc*_*tua 11

它只是点,你不能在shell函数名称中使用点.或者任何变量名称.

我将链接到这个问题:linux环境变量名中的允许字符

  • 在POSIX shell中,函数[name](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_231)必须仅包含下划线和字母数字字符. (3认同)