Sample Header Ad - 728x90

MSYS2: Command works fine in terminal, but syntax error `(' in script

0 votes
1 answer
401 views
On Windows, with MSYS2, I want to add the file extension for an executable back to the output of which, if it has one. Here's a one-liner:
locAlt="$(which "mingw32-make")" ; echo "$locAlt" ; locAlt=("$locAlt"+(|.*)) ; echo "$locAlt"
When run in the MSYS2 MinGW terminal, this works just fine on my machine and prints:
/mingw64/bin/mingw32-make
/mingw64/bin/mingw32-make.exe
However, now I put the same line into a script (one-liner.sh):
#/bin/bash
locAlt="$(which "mingw32-make")" ; echo "$locAlt" ; locAlt=("$locAlt"+(|.*)) ; echo "$locAlt"
I tried both nano and Notepad++ (where you can choose line endings, and they're set to "UNIX (LF)", and checked the output of cat -v one-liner.sh, which shows the file as above (no ^M, as in this question ). However, when trying to run the file with ./one-liner.sh, I get the error
./one-liner.sh: line 2: syntax error near unexpected token `('
./one-liner.sh: line 2: `locAlt="$(which "mingw32-make")" ; echo "$locAlt" ; locAlt=("$locAlt"+(|.*)) ; echo "$locAlt"'
Why could that be?
Asked by RL-S (141 rep)
Aug 12, 2022, 07:33 AM