I came across a Bash script today that has function names with double colons :: in them, e.g., file::write() and file::read(). I've never seen this syntax before in a Bash script, and when I invoked the script it ran just fine (to my surprise).

After scouring Bash's man page on my system (and online) I cannot find anything in the documentation that supports this syntax for function names. For example, the section Shell Defined Functions defines the syntax for a shell function to be

function name [()] compound-command [redirection]

and then (elsewhere in the manual) the token name is defined as

name   A word consisting only of alphanumeric characters and
       underscores, and beginning with an alphabetic character
       or an underscore.  Also referred to as an identifier.

There's no mention anywhere of the double colon syntax for function names.

The only other reference to this double colon syntax that I've found thus far is in this Shell Style Guide (see the subsection Naming Conventions > Function Names) which recommends using the double colon syntax for function names in "packages"--e.g., mypackage::myfunction().

Is this double colon syntax for function names a legitimate feature of the Bash shell, or is it perhaps an undocumented feature? If it's legit, where is it documented in the Bash manual? I've looked and looked but I can't find anything about it in the manual. The closest I've found is the use of :: in the PATH environment variable to add the current working directory to the search path.

EXAMPLE

#!/bin/bash
function abc::def() {
    echo "${FUNCNAME[0]}"
}
abc::def

I tested this script on three different Linux distros, and on all three the script prints abc::def to stdout.

up vote 15 down vote accepted

This is a case of the documentation being stricter than the implementation, possibly in an attempt to lower the footgun factor. This has been discussed here before; see also the exhaustive test establishing that for example [}{ is a valid function name.

It may also be worth noting that abc::def is not a valid variable name:

$ abc::def=foo
bash: abc::def=foo: command not found
  • 3
    just to make it vividly clear - not file::read nor [}{ is valid posix – Steven Penny Aug 20 at 13:27

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.