Re: Characters in function names



On Fri, 15 Feb 2008 04:40:33 -0800 (PST), Spiros Bousbouras wrote:
On Feb 11, 1:53 pm, Stephane Chazelas <stephane_chaze...@xxxxxxxx>
wrote:
On Mon, 11 Feb 2008 03:04:32 -0800 (PST), Spiros Bousbouras wrote:
Which characters can appear in function names
in BASH ? Other shells ?

As they share the same namespace as command arguments, intuition
would say any character but the NUL character, but YMMV.

But command arguments don't have a namespace meaning that
the shell doesn't store them anywhere. Or do you mean command
names ? If you mean command (as in executable) names then
it's not completely the same with function names because
with functions the shell has to be able to also parse the
definition correctly. But since you're saying that zsh allows
anything I guess it is possible.

By name space, I probably meant more something like syntax.

The command to execute is determined from the first argument.

In:

'a b' 'b
d' ''

you have 3 arguments. Those 3 arguments are passed to a command
which is identified thanks to the first one. It might be a
function defined as 'a b'() { ... } or "/usr/bin/a b".


zsh allows everything. Even the empty string or the NUL
character (zsh is the only shell to allow NUL bytes where
applicable) are valid function names.

I wonder how one defines or calls a function whose
name only contains NUL bytes.

$'\0\0'() echo test

or

^@^@() echo test

(Where ^@ are actual NUL characters).

Then you may type:

<Ctrl-Space><Ctrl-Space><Return>

to invoque it for instance.

Other shells may have restrictions, but they would be artificial
as there's no reason at all to put a restriction here.

An argument could be made for not allowing = in a function name
because if you type something like t=5 it's not clear if you mean
an assignment or you want to execute a function called t=5.

= is allowed in command names in every shell. You just need to
escape the "=" so that it's not taken as an assignment.

The
same ambiguity can arise with executable names but executable names
are up to the filesystem and a shell has no control over that.

Yes, so given that there's no limitation for commands, there's
no reason to put any for function names.

--
Stephane
.