mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
6.98k stars 332 forks source link

syntax: bash allows more characters in function names #1013

Open chnkd opened 1 year ago

chnkd commented 1 year ago
function foo[foo] { # "function" must be followed by a name
    echo foo
}
bar[bar]() { # "a[b]" must be followed by =
    echo bar
}
run=(foo[foo] bar[bar]) # "[x]" must be followed by =
${run[0]}
${run[1]}
mvdan commented 1 year ago

foo[foo] and bar[bar] are just func names there, right? Out of curiosity, why would you use names that look like array indexing expressions? :)

I've personally never come across this in practice. If Bash supports it, we probably should support it as well, although it's worth noting that POSIX shell defines names (like function names) as simply:

In the shell command language, a word consisting solely of underscores, digits, and alphabetics from the portable character set. The first character of a name is not a digit.

Meaning that such func names are not valid POSIX Shell.

man bash, instead, says:

When in posix mode, fname must be a valid shell name and may not be the name of one of the POSIX special builtins. In default mode, a function name can be any unquoted shell word that does not contain $.

Pretty bonkers design decision from Bash if you ask me!