Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC1036 for function declaration "==== () { echo test; }" #2047

Open
kbauer opened this issue Aug 28, 2020 · 5 comments
Open

SC1036 for function declaration "==== () { echo test; }" #2047

kbauer opened this issue Aug 28, 2020 · 5 comments

Comments

@kbauer
Copy link

kbauer commented Aug 28, 2020

How to reproduce

Give ShellCheck the following script:

#!/usr/bin/env bash
==== () { echo "HEADING: $1"; }

Seen output

Line 2:
==== () { echo "HEADING: $1"; }
     ^-- SC1036: '(' is invalid here. Did you forget to escape it?
     ^-- SC1088: Parsing stopped here. Invalid use of parentheses?

Expected output

None. In Bash scripts, ==== is a valid identifier. This can be used e.g. to combine structuring a script with headings and producing progress output at the same time.

However, it looks like this is very specific to Bash. Both ksh and sh complain about different syntax errors caused by an invalid identifier.

@matthewpersico
Copy link

@kbauer
Copy link
Author

kbauer commented Aug 28, 2020

It's really not entirely clear; The discussion also shows that this interpretation of the documentation doesn't match the actual behavior of bash, with backwards-compatibility requirements making future changes unlikely. Bash does however reject the identifier when invoked as bash --posix. So maybe it should be allowed, but warned about?

On the other hand, ShellCheck doesn't catch the use of an invalid identifier in:

#!/usr/bin/env ksh
hello-world () { :; }

@koalaman
Copy link
Owner

koalaman commented Sep 1, 2020

As a workaround, you can use function ==== () { echo "HEADING: $1"; }.

The general problem has been that bash accepts a lot of strange names, e.g. **=??!, and ShellCheck tends to give misleading advice when any such strings could be considered the start of a function.

To avoid suggesting "Maybe you're missing () in a function" whenever you write random gibberish, it therefore only allows particularly weird characters after a function keyword even though bash doesn't care as much.

@matthewpersico
Copy link

On the other hand, ShellCheck doesn't catch the use of an invalid identifier in:

#!/usr/bin/env ksh
hello-world () { :; }

Assuming you are talking about the function name, shellcheck allows hello-world probably because, although invalid according time the docs, it is works in bash to have dashes in function names. Not identifiers though. If you’re talking about the :;, then disregard.

@koalaman
Copy link
Owner

koalaman commented Sep 2, 2020

It does warn for sh and dash as part of the bashism checks. It's simply missing the same for ksh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants