shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

Optional check: function declarations should not have `function` prefix

Open felipecrs opened this issue 3 years ago • 7 comments

EDIT: I had initially proposed the opposite, but I changed my mind about this. See the conversation below.

This is to ensure a consistent code style. Example:

function myfunction() {
  true
}

Should be:

myfunction() {
  true
}

Original issue description

This was before I changed my mind.

This is to ensure a consistent code style. Example:

myfunction() {
  true
}

Should be:

function myfunction() {
  true
}

felipecrs avatar Dec 23 '22 19:12 felipecrs

I came here to actually suggest the opposite of this.

according to This SO post; the use of:

function myfunction() { ... ; }

is completely wrong, and shouldn't be used; and that the expected syntax is:

myfunction(){ ...; }

I'm not confident in this, but would enjoy seeing the resulting conversation around correctness here ;)

wolfspyre avatar Jul 24 '25 05:07 wolfspyre

This seems like a "try it and see" kind of question to me. :-)

~ $ bash --version GNU bash, version 5.3.0(1)-release (aarch64-unknown-linux-android) ~ $ function bla2 () { printf '<%s>\n' "$@";} ~ $ bla2 4 5 76 <4> <5> <76> ~ $ unset -f bla2 ~ $

Perhaps this 'function word ()...' syntax causes an error in some other shell?

On Wed, Jul 23, 2025, 22:24 Wolf Noble @.***> wrote:

wolfspyre left a comment (koalaman/shellcheck#2645) https://github.com/koalaman/shellcheck/issues/2645#issuecomment-3112001295

I came here to actually suggest the opposite of this.

according to This SO post https://stackoverflow.com/questions/22238033/should-i-define-a-shell-function-as-function-x-or-just-x; the use of:

function myfunction() { ... ; }

is completely wrong, and shouldn't be used; and that the expected syntax is:

myfunction(){ ...; }

I'm not confident in this, but would enjoy seeing the resulting conversation around correctness here ;)

— Reply to this email directly, view it on GitHub https://github.com/koalaman/shellcheck/issues/2645#issuecomment-3112001295, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUF2F25WNXNIPEU7LHEAA433KBUYTAVCNFSM6AAAAACCHXCMO6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTCMJSGAYDCMRZGU . You are receiving this because you are subscribed to this thread.Message ID: @.***>

wileyhy avatar Jul 24 '25 18:07 wileyhy

erm... no?

Just cause somethin works doesn't mean it's necessarily a good idea :)

according to the docs 'function' AND '()' aughtn't be used in conjunction. bash and zsh enable it, but it's not proper, strictly speakin.... will it work? sure. but if that's all one cared about, why would one use shellcheck in the first place? :)

Since we're talking about a tool which is intended to suggest patterns which reflect current best practices I'd hope that effaroundandfindout isn't the de-facto mechanism of evaluating the merit of a pattern...

:)

but I do admit this is kinda nit-picky...

wolfspyre avatar Jul 24 '25 18:07 wolfspyre

I agree with you, @wolfspyre, f() { echo hello world; } should be the preferred one. I updated the issue description to make this clearer.

Also referenced in https://mywiki.wooledge.org/Bashism.

felipecrs avatar Jul 24 '25 18:07 felipecrs

Well, there are some varying levels of "proper" under consideration.

Would 'function word ()...' conform to [pick your favorite corporation's] Style Guide? Maybe / maybe not. That's up to them.

Is said syntax a shell error, meaning, could such syntax cause, for instance

  • a backup script to silently fail,
  • data to be read or processed incorrectly,
  • unexpected side effect(s) to occur, or
  • for one to "incur the wrath from high atop the thing?"

Case in point:

~ $ type -a '!' ! is a shell keyword ~ $ function '!' () { printf '%d\n' "${LINENO}";} bash: `'!'': not a valid identifier ~ $

In my opinion, if people claim some syntax is "wrong," it may be worthwhile to ask for whom and when.

On Thu, Jul 24, 2025, 11:09 Wolf Noble @.***> wrote:

wolfspyre left a comment (koalaman/shellcheck#2645) https://github.com/koalaman/shellcheck/issues/2645#issuecomment-3114383678

erm... no?

Just cause somethin works doesn't mean it's necessarily a good idea :)

according to the docs 'function' AND '()' aughtn't be used in conjunction. bash and zsh enable it, but it's not proper, strictly speakin.... will it work? sure. but if that's all one cared about, why would one use shellcheck in the first place? :)

Since we're talking about a tool which is intended to suggest patterns which reflect current best practices I'd hope that effaroundandfindout isn't the de-facto mechanism of evaluating the merit of a pattern...

:)

but I do admit this is kinda nit-picky...

— Reply to this email directly, view it on GitHub https://github.com/koalaman/shellcheck/issues/2645#issuecomment-3114383678, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUF2F22RQQ5IT4OK3LZIST33KEOMJAVCNFSM6AAAAACCHXCMO6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTCMJUGM4DGNRXHA . You are receiving this because you commented.Message ID: @.***>

wileyhy avatar Jul 24 '25 18:07 wileyhy

That's why this ask is for an optional check, like these:

  • https://www.shellcheck.net/wiki/SC2250
  • https://www.shellcheck.net/wiki/SC2248
  • https://www.shellcheck.net/wiki/SC2292

felipecrs avatar Jul 24 '25 19:07 felipecrs

I would like a optional check for this indeed. (on team "don't use the function keyword" =))

brother avatar Aug 13 '25 09:08 brother