I encountered a strange bug today, when running a script in a directory containing a directory with parentheses in it, such as a()
.
Minimal working example
I managed to reduce the bug to the following minimal working example:
Create an empty directory in /tmp
and cd
into it:
mkdir /tmp/foo
cd /tmp/foo
Create a script named foo.sh in it containing:
foo() {
somevar=1;
case somevar in
aha) echo "something" ;;
*) echo "other" ;;
esac;
};
Run the following command:
eval $(/bin/cat foo.sh)
There should not be any error.
Create a file with parentheses:
touch "a()"
Run the command again:
eval $(/bin/cat foo.sh)
I now get the error:
bash: syntax error near unexpected token `('
Why does bash even care about what files are in the directory? Why do parentheses cause an error?
System information:
$ 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
More detailed background and original error:
My problem came from using a script sourcing /usr/share/modules/init/bash
from the environment-modules
package, as summarized here:
$ 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='' ;;'