Shellcheck: error| syntax error near unexpected token `('

Created on 18 Jan 2020  路  10Comments  路  Source: koalaman/shellcheck

For bugs

  • Rule Id (if any, e.g. SC1000): NA
  • My shellcheck version (shellcheck --version or "online"): 0.7.0
  • debian package version: 0.7.0-2
  • [X] The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • [X] I tried on shellcheck.net: appears to have been fixed in repo
  • including report for reference

Here's a snippet or screenshot that shows the problem:

#!/bin/bash

shopt -s extglob;
for f in /etc/hosts/host\.!(mail)\.conf ; do
    echo "$f";
done;

Here's what shellcheck currently says:

error| syntax error near unexpected token `('

Here's what I wanted or expected to see:

no error at all.

Most helpful comment

my memory lane is slow, but drop-out is fast:
re-hit this bug, wanted to report the bug, found my previously created issue..

All 10 comments

  • appears to have been fixed in repo
  • including report for reference

my memory lane is slow, but drop-out is fast:
re-hit this bug, wanted to report the bug, found my previously created issue..

Are you using Ale? I think this is an error emitted by bash -n, not Shellcheck.

@bewuethr wazzat, Ale? -n? never used -n. Care to elaborate on this?

Sorry, didn't give any context. Ale is a Vim plugin that lets you asynchronously run linters. For shell scripts, it defaults to shellcheck and bash -n for linting. -n is described like this in the manual:

Read commands but do not execute them. This may be used to check a script for syntax errors. This option is ignored by interactive shells.

So, for a minimal script with an extended "except" glob

$ cat poc
#!/usr/bin/env bash

shopt -s extglob
echo !(foo)

shellcheck doesn't complain, but bash -n does, with the exact error message you're mentioning above:

$ shellcheck poc
$ bash -n poc
poc: line 4: syntax error near unexpected token `('
poc: line 4: `echo !(foo)'

That's why I'm thinking that this isn't a shellcheck message. The shellcheck codebase doesn't include "unexpected token" anywhere, as far as I can tell.

Whoa. Thanks. But. No. Not using Ale. Straight shellcheck only.

Can escape the glob for shellcheck to make it happy. Won't execute, though. What will execute, won't pass shellcheck.

But how do you run shellcheck? If I paste the snippet from your question, neither shellcheck.net nor my locally installed shellcheck complain. How can I reproduce what you see?

  • shellcheck.net has a fix already merged
  • your local shellcheck version may also have the fix merged

Try to downgrade to a shellcheck version prior to the merge, i.e. 0.7.0 (see my initial report).

I use 0.7.0, which seems to be the most recent release :man_shrugging:

Now, _that_ is interesting. What distribution package are you using then?

UPDATE:

(darn, my previous edit got lost in space)

Tested using

shellcheck test.sh

No error.

Opening test.sh with the conflicting content in post #1 results in the error given above.

So it seems shellcheck is indeed not the culprit here but rather the vim-shellcheck plugin.

Thanks @bewuethr for making me realise that shellcheck is not to be blamed here. Heading off to vim-shellcheck, then.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  4Comments

erwinkramer picture erwinkramer  路  5Comments

ymkjp picture ymkjp  路  3Comments

balloonpopper picture balloonpopper  路  4Comments

nathaniel112 picture nathaniel112  路  4Comments