簡體   English   中英

bash 腳本中帶有“let”命令的意外標記 `(' 附近的語法錯誤

[英]syntax error near unexpected token `(' with "let" command in bash script

我有一個 bash 腳本如下,它會生成伴隨的錯誤:

# cat ./a.sh
#!/bin/bash
let C=1+(2)
echo $C
#
# ./a.sh
./a.sh: line 2: syntax error near unexpected token `('
./a.sh: line 2: `let C=1+(2)'

但是,它可以從命令行按預期工作:

# let C=1+(2)
# echo $C
3

如果我將腳本中的“let”表達式更改為以下內容,它也可以工作:

C=$((1+(2)))

我的bash版本如下:

# /bin/bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

操作系統是 CentOS 7.7

啊,這是罪魁禍首:在交互式 session 中:

$ declare -p BASH_VERSINFO
declare -ar BASH_VERSINFO='([0]="4" [1]="2" [2]="45" [3]="1" [4]="release" [5]="i386-apple-darwin19.5.0")'

$ let C=1+(2) && echo $C
3

$ shopt -u extglob

$ let C=1+(2) && echo $C
bash: syntax error near unexpected token `('

+(pattern)是一個擴展的 glob 模式,當關閉extglob時,它顯然是非法的語法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM