且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Bash脚本错误:"功能:找不到&QUOT ;.为什么会出现?

更新时间:2022-06-09 08:27:51

有机会,您的桌面上,你实际上并没有在庆典而是破折号或其他兼容P​​OSIX的外壳,不承认函数关键字。在函数关键字是一个bashism,一个bash扩展。 POSIX语法不使用函数并授权使用括号。

Chances are that on your desktop you are not actually running under bash but rather dash or some other POSIX-compliant shell that does not recognize the function keyword. The function keyword is a bashism, a bash extension. POSIX syntax does not use function and mandates the use of parenthesis.

$ more a.sh
#!/bin/sh

function sayIt {   
   echo "hello world"
}

sayIt
$ bash a.sh
hello world
$ dash a.sh
a.sh: 3: function: not found
hello world
a.sh: 5: Syntax error: "}" unexpected

在POSIX语法工作在两个:

The POSIX-syntax works in both:

$ more b.sh
#!/bin/sh

sayIt () {   
   echo "hello world"
}

sayIt
$ bash b.sh
hello world
$ dash b.sh
hello world