bash语法错误,出现在意外标记'('旁,在脚本[重复]中使用通配符的括号时

问题描述 投票:-1回答:1

我正在尝试运行此脚本:

mkdir a
cd a
touch a1, a2, a_
ls ./a!(_)
echo -e '#!/bin/bash\nls ./a!(_)\n' >> run.sh
chmod a+x run.sh
./run.sh

ls行按预期方式打印输出(./a1, ./a2,),但是脚本失败并显示:

./run.sh: line 2: syntax error near unexpected token `('
./run.sh: line 2: `ls ./a!(_)'

是否可以在不使用findfor的情况下在bash脚本中使用括号?

regex linux bash shell wildcard
1个回答
2
投票

您需要使用bash启用extglobshopt -s extglob选项才能使用否定模式。

echo行更改为

printf '%s\n' '#!/bin/bash' 'shopt -s extglob' 'ls ./a!(_)' > run.sh

之后您可以使用shopt -u extglob禁用扩展的glob。

相关:

© www.soinside.com 2019 - 2024. All rights reserved.