为什么在函数定义中的大括号之前需要空格?

Giu*_*rdi 2 syntax bash function

我正在尝试创建一个bash脚本,将一堆pdf转换为文本以提取一些信息,但shell给了我这个错误:

./AutoBib.sh: line 8: syntax error near unexpected token `pdftotext'
./AutoBib.sh: line 8: `    pdftotext $1 temp.txt'
Run Code Online (Sandbox Code Playgroud)

这里有一个我的功能的例子:

function doi{

    pdftotext $1 temp.txt
    cat temp.txt | grep doi: | cut -d: -f 2 | head -n 1 >> dois.txt
    rm -rf temp.txt
}
doi $PDF
Run Code Online (Sandbox Code Playgroud)

变量PDF在输入中的位置.在添加它起作用的函数之前,我曾经写过我的脚本:

pdftotext $PDF tempo.txt
Run Code Online (Sandbox Code Playgroud)

cod*_*ter 5

从Bash 手册:

大括号是保留字,因此它们必须通过空格或其他shell元字符与列表分开.

function ...是用于定义Bash函数的过时语法.请改用:

doi() {
   ...
}
Run Code Online (Sandbox Code Playgroud)

由于()是元字符,在这种情况下你不需要空格(虽然空格使你的代码更漂亮):

doi(){
  ...
}
Run Code Online (Sandbox Code Playgroud)

稍微扩展一下,记住{在命令分组中的 '}'之前和之前我们需要一个空格(空格,制表符或换行符),如下所示:

{ command1; command2; ... }
Run Code Online (Sandbox Code Playgroud)