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

syntax: bash allows more characters in function names #1013

Open
chnkd opened this issue Jun 23, 2023 · 1 comment
Open

syntax: bash allows more characters in function names #1013

chnkd opened this issue Jun 23, 2023 · 1 comment

Comments

@chnkd
Copy link

chnkd commented Jun 23, 2023

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
Copy link
Owner

mvdan commented Jun 25, 2023

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!

@mvdan mvdan changed the title declare func with associative array syntax: bash allows more characters in function names Jun 25, 2023
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

2 participants