20

Problem in short - In Linux, whenever we get the following error "Syntax error: word unexpected (expecting ")")", what does it generally mean?

Problem in details - I have been trying to cross-compile Qt 4.6 as per the Sourcery tool chain on Ubuntu 10.04 (Lucid Lynx). I followed the exact steps mentioned at the link compiling Qt-4.6. But I get the following error right in the ./configure step -

/home/weds/qt-everywhere-opensource-src-4.6.1/bin/qmake: 1: Syntax error: word unexpected (expecting ")")

Searching on the Internet I found lots of posts regarding this error and read all of them. What is this error and how can I solve it?

P.S 1 - the Sourcery toolchain is present inside /opt/ folder and my PATH variable is correctly pointing to it.

P.S 2 - This toolchain was not installed manually by me. Rather it was provided by a vendor as a .tgz file which I extracted inside the /opt/ folder.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • This error also happens if you try to run an ELF executable compiled for the wrong arch with `exec`: https://stackoverflow.com/questions/22460589/armv8-running-legacy-32-bit-applications-on-64-bit-os/51466709#51466709 Related: https://unix.stackexchange.com/questions/224040/syntax-error-word-unexpected-expecting-when-running-remotely-but-no-probl – Ciro Santilli OurBigBook.com Jul 22 '18 at 18:35
  • What is the *"Sourcery tool chain"*? Is it [Sorcerer](https://en.wikipedia.org/wiki/Sorcerer_(Linux_distribution)) (a Linux distribution. Different spelling)? Or something else? (*Sorcerer*, *sourceror*, *sourcerer*, *sorcery*, and *sourcery* are heavily overloaded words.) – Peter Mortensen Nov 27 '21 at 22:54

7 Answers7

15

That's an error reported by the Almquist shell or any of its derivatives like Dash (and Dash happened to be the default implementation of /bin/sh on Ubuntu 10.04 (Lucid Lynx)) when a word is found while parsing the syntax of a script where a ) is expected instead, for instance like in this case statement:

dash -c 'case a in b c) :; esac'

dash: 1: Syntax error: word unexpected (expecting ")")

That's because after b , the only thing that is expected after is ), (though actually | would also be allowed) so that c word is unexpected.

dash -c 'myfunc( something'

dash: 1: Syntax error: word unexpected (expecting ")")

One case where that can happen is if the script has been written on or transferred through a Microsoft OS where text line endings are CRLF instead of just LF.

A

case a in b) cmd1;;
          c) cmd2
esac

script written on MS-DOS would appear as:

case a in b) cmd1;;<CR>
          c) cmd2<CR>
esac<CR>

on Unix and that c would be an extra word after the <CR> word.

Here that's unlikely as your error reports the problem being on the first line of the script and most scripts start with the #! /path/to/interpreter shebang line.

Another possibility is that that script you're trying to run has been written on the assumption that sh was bash and uses constructs that are not portable to other sh implementations.

Since you're using an outdated and no longer maintained OS, it's also possible that you're running into a bug in that version of Dash. You could run dpkg-reconfigure dash and tell the system not to use Dash for sh (but Bash instead) to see if that helps.

Again, it is unlikely to be on the first line.

What sounds more likely is that that qmake file is not a script, but a binary executable that is not recognised as such by the system, for instance because it is of a binary format for the wrong architecture or it has been corrupted in transfer.

In that case, when the system fails to recognise it as a native executable, the invoking application would try to run sh on it as if it was a shell script, and the presence of a ( character in the file could cause Dash to fail with such an error.

On my system:

dash /bin/touch

/bin/touch: 1: /bin/touch: Syntax error: word unexpected (expecting ")")

And if you look at the content of /bin/touch as if it were a script, you see:

^?ELF^B^A^A^@^@^@^@^@^@^@^@^@^B^@>^@^A^@^@^@5&@^@^@^@^@^@@^@^@^@^@^@^@^@(ô^@^@...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stephane Chazelas
  • 5,859
  • 2
  • 34
  • 31
  • Re *"Dash happened to be the default implementation of /bin/sh on Ubuntu 10.04"* (2010): That still seems to be the case - at least as observed for [Ubuntu MATE 20.04](https://en.wikipedia.org/wiki/Ubuntu_MATE#Releases) (Focal Fossa, 2020. With [Cinnamon](https://en.wikipedia.org/wiki/Cinnamon_(desktop_environment)).). `/bin/sh` points to Dash (`dash`). `ls -l /bin/sh` outputs `lrwxrwxrwx 1 root root 4 Jun 1 2020 /bin/sh -> dash`. – Peter Mortensen Nov 28 '21 at 19:20
  • It makes more sense with the historical trivia that Dash is for *Debian Almquist shell*. – Peter Mortensen Nov 28 '21 at 19:24
  • @PeterMortensen yes, Ubuntu moved from bash to dash for /bin/sh in 6.10 (long before Debian actually) and hasn't moved back since (details are in the Wikipedia link that you added; thanks for that and other improvements) – Stephane Chazelas Nov 28 '21 at 19:28
  • Re "if the script has been written on or transferred through a Microsoft OS where text line endings are CRLF instead of just LF." Thx! This helped me out, working remote on Rasperry Pi 3 from VSC/Win10 – Hans Ratzinger Apr 27 '22 at 09:12
4

In 99% of the cases it is a wrong file transfer mode, ASCII or binary.

Try to extract the toolchain directly on the target system.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    I'd have suggested this too, but the OP says the `.tgz` was extracted directly into the `/opt` folder. – roaima Apr 19 '16 at 16:23
  • 1
    Thanks, solved by this answr , I have compressed binary to .tar.gz and after file transfer I extracted to the target device. Erro is due to file tansfr problm – TamiL Jun 15 '20 at 07:00
4

An answer to this seems to be posted in the instructions to which you linked.

Admittedly it's a long way down in the comments but it didn't take long to search for qmake: Syntax error: word unexpected.

Quote:

Tej says: January 4, 2013 at 12:20 pm

Ok, I have solved the Problem. Its very unfortunate that ppl did not tell what actually is the problem. Problem is we have to use Host qmake. For that whatever Export (export PATH=/usr/local/arm/4.3.2/bin:$PATH etc.) we did in step during tslib installation, we have to undo all of that. Thats it.

Hope that help someone

In case that's not clear, Tej suggests that it would seem that you're trying to run the cross-compiled qmake on the host system.

roaima
  • 588
  • 7
  • 27
  • Thanks a ton Roaima for pointing this out. I had read this comment but somehow had assumed that it was a PATH error, so ignored it. But I again re read after you pointed it out and bingo, that was the problem :) But now I am getting another problem, I am posting that as a new issue. – satya prakash Panigrahi Apr 20 '16 at 12:35
1

I was writing a C++ program on Ubuntu 18.04 (Bionic Beaver) when this problem came around. The cause was that the file name of the program contained the characters "(" and ")". After renaming the files, it worked for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

This error can also be thrown when calling a NodeJS script from a shell script, without providing the correct shebang line in NodeJS:

#!/usr/bin/env node

// Rest of your NodeJS script
ojathelonius
  • 685
  • 8
  • 26
0

A weird fix for me was deleting file package-lock.json and the node_modules folder, and then building the Docker image again.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gage
  • 61
  • 2
  • 7
  • How is this related to the question? What is going on in that Docker image? What is it used for? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/67098855/edit), not here in comments (but ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Nov 27 '21 at 23:14
0

Sometimes this error occurs just because the directory in which you are currently working has "wrong naming convention" .. like it could be

  1. demo_project (copy)
  2. my_project_1 (another copy)

The correct naming convention says
1. demo_project_copy
2. my_project_1_another_copy

Nikhil
  • 1