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>
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.


'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


^@^@() echo test

(Where ^@ are actual NUL characters).

Then you may type:


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.

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.


Relevant Pages

  • Re: Search and replace in one field
    ... where you don't wnat the " replaced by another character. ... the s command is the substitute command. ... However " is meaningful to the shell but preceding a character with a backslash tells the shell to ignore it so where the line contains \" the shell removes the \ and passes the " to the command. ... So what sed sees as its first parameter is s/"//g which tells it to replace all occurrences of " with nothing. ...
  • Re: Excercices
    ... > example or get a better shell. ... program (in cmd.exe it is an internal command), ... In the above example there is neither a escape character nor ... You are called "Phil" so you can be distinguished from other ...
  • Bash-4.0 available for FTP
    ... Unlike previous bash distributions, this tar file includes the formatted ... The shell has been changed to be more ... rigorous about parsing commands inside command substitutions, ... Changes have been made to the Readline library being released at ...
  • Re: () $() etc., in Bash
    ... It is replaced on the command line by the output of the command ... different from one shell to another) on the character string ... linefeed character has been removed. ...
  • Why newbies dont RTFM...
    ... Even though I've used Linux before, I've never had to do any ... BASH BUILTIN COMMANDS ... last command exited within ... unless the shell is not exeâ ...