我今天遇到一个奇怪的错误,在一个目录中运行脚本时,该目录中包含一个带括号的目录,例如a()
.
最小的工作示例
我设法将错误减少到以下最小的工作示例:
/tmp
在其中创建一个空目录cd
:
mkdir /tmp/foo
cd /tmp/foo
在其中创建一个名为foo.sh的脚本,其中包含:
foo() {
somevar=1;
case somevar in
aha) echo "something" ;;
*) echo "other" ;;
esac;
};
运行以下命令:
eval $(/bin/cat foo.sh)
不应该有任何错误。
创建一个带括号的文件:
touch "a()"
再次运行命令:
eval $(/bin/cat foo.sh)
我现在得到错误:
bash: syntax error near unexpected token `('
为什么 bash 甚至关心目录中的文件?为什么括号会导致错误?
系统信息:
$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright © 2016 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.
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.1 LTS
Release: 18.04
Codename: bionic
更详细的背景和原始错误:
我的问题来自使用/usr/share/modules/init/bash
从environment-modules
包中获取的脚本,总结如下:
$ dpkg -l environment-modules
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-=============================================================-===================================-===================================-================================================================================================================================
ii environment-modules 4.1.1-1 amd64 Modular system for handling environment variables
$ source /usr/share/modules/init/bash
$ touch "a()"
$ source /usr/share/modules/init/bash
bash: eval: line 43: syntax error near unexpected token `('
bash: eval: line 43: ` a() _mlshdbg='' ;;'