The Standard Function
Table of Contents
If you're wondering how to read these documents, see the discussion in the References, under Where To Start.
1 The Standard Function
1.1 Introduction
A standard for writing bash shell functions is presented. Requirements are established for function presentation and syntax. A canonical form is established. Reserved behavior is identified; features are advanced for assertion checking, self-documentation and instrumentation. A naming convention is presented to identify related functions and to identify test functions.
Other papers make a collection: The Library Standard, to collect functions in a shell library, and The Standard Function Library, a standard library of standard functions supporting the preferable, and reserved behaviors. Other papers (see 1.5) support these standards.
The requirements, reserved behavior and naming conventions are used by the author. Examples from existing functions are shown.
1.2 Definition
- The canonical form of the standard function is produced by the
declare -f
builtin:
$ declare -f standard_function
standard_function ()
{
...
}
1.3 Requirements
Standard functions:
- shall be preserved in the canonical format
- shall use alpha-numeric names of three or more characters where, in the function name, a leading underscore is only permitted in the scope of another function.
- shall use only the null operator
:
as a comment (implicit in the first requirement ) - preferably will check assertions about arguments as to number and type
- preferably may use extended features of the null-operator comment
A function meeting these requirements is a standard function
The latter two preferences are supported by functions and applications referenced in The Standard Function Library
1.3.1 Function names
A function name is preferably lower case. Where the function contains an underscore, the word preceding the underscore is considered to define a family of functions. The word or words after the underscore identify a subfunction in the family. A single function, with no underscore, shares the family name, serves as the main function of the family. This raises the notion of entry points, functions called from the command line. These represent the user interface to the family of functions.
1.3.2 Arguments
The standard function preferably makes assertion-checking statements about it's various arguments, returning distinct error conditions for assertion failures.
The list of conditions includes:
- minimum number of arguments
- those arguments expected to be a file
- those arguments expected to be a function
- those arguments expected to be an identifiable type of object
- any result of a test statement (specific file types, string pattern, …)
1.3.3 Comments
- Since an arbitrary function, rendered in it's canonical form loses any sharp # comments, the null operator : is the required comment.
- Semantic restrictions using the null operator exist and must be avoided
- The null operator may be used to reserve information behaviors.
$ standard_function () { # this comment does not survive the canonical format; return; } $ declare -f standard_function # produces standard_function () { return } ... $ standard_function ( ) { : this comment SURVIVEs the canonical format; return ; } $ declare -f standard_function standard_function () { : this comment SURVIVEs the canonical format; return } $
1.3.4 Reserved Behavior
Reserved behavior identifies syntactic or meta-syntactic behavior reserved for information such as shdoc a Shell Documentation standard. A reserved behavior is introduced by a null-command tag.
1.3.5 the tag
A tag feature enables the function to hold meta-information
standard_function ()
{
: {tag}: {information}
}
A companion, the Tag Manual is the user's guide to tags. A handful have a standard practice; many suggest further development.
standard_function ()
{
: tag: a sample tag, such as;
: date: 2019-12-24 -- last modified
: ...
...
}
1.3.6 semantic comment
The functions comment, ignore, quietly alter the behavior of stdout, stderr to reflect their names, and are covered in The Standard Function Library
1.3.7 shdoc
A function's first untagged null-comments are the shdoc for the function, a brief description of the function's behavior. The first of these is considered the function abstract.
standard_function () { : the shdoc of a standard function, this line is the function abstract: : and further lines describe explicit behavior. : tag: a sample tag, such as; : date: 2019-12-24 -- last modified : ... ... }
Shell documentation is supported with the shdoc function (SHell DOCumentation). The function and function family are covered in The Standard Function Library
1.4 Naming Conventions
The concept of a function family, a group of functions sharing a common initial stem, will require certain naming conventions. Independent of function families, some functions behavior may be sufficiently generic that no family name is appropriate. And some subfunction names may be quite general in service of the family that they may have requirements levied against them. A local function may be defined within a function. It's scope is available to the current shell until it is unset
The function practice document discusses these categories and introduces reserved names in these categories.
- family names
- general utility functions
- common subfunction names
- local functions, internal to other functions with conventional semantics.
1.4.1 Default Arguments
The standard function is encouraged to use a convenient default for any argument when supplying the default exhibits an effect to describe or exhibit the function's behavior.
In the body of a function, this is how a default argument is assigned:
standard_functions () { ... local var=${1:-defaultValue} ... }
See The Standard Function Library for examples of use.
1.4.2 Assertions
An alternate semantics is open for discussion. Rather than the current
report_not...condition... && return N
possibly a more familiar form might be:
assert_is...condition... || return N
An assert_is function would necessarily return the complementary status of the report_not function.
1.5 References
1.5.1 Where to Start
The practice documented here is all about developing bash shell functions. In my opinion, the use of functions in bash, while readily accessible, is under-appreciated, under-used. Pick one of these approaches to the material, based on your needs and interests.
- Formal Approach
If you are interested in a more formal approach, choose this reading sequence:
- the Standards Documents
- then the Application Requirements
- lastly, the Practice Documents
The Tag Manual discusses uses of a new feature introduced here, the null-command comment. The tag was introduced mostly to provide the shdoc feature, internal documentation, motivated by traditional languages, javadoc, pydoc, …
- Shell Programmer
If you have some shell programming experience, choose this order:
- the Practice Documents
- then the Application Requirements
- lastly, the Standards Documents
- Gaining Experience
While the material here is not for the beginning shell programmer, the author recommends his e-book Shell Functions, which has a link to a series of YouTube videos on the method. Visiting the videos, you will find a host of other introductions to the shell.
- Your entry point will be the Practice Documents
- followed by any useful sequence.
1.5.2 Definitions
Three definitions define sets of functions, distinguishing the need for the collection: the app, family, and library. The library is the most general, the app being the most specific. The relationship is discussed in The Standard Function Library.
Where these definitions appear in requrements documents, they assume a certain implementation, in particular the definition of a tag. Since the null-operator, a gift from the shell designers, uses the colon, it seems appropriate to use the colon as the distinguishing feature of a tag, .e.g.
date: 2020-02-20 this change ....
- app – a set of functions collected to serve a set of requirements
- canonical function – a function when formatted
declare -f function_name
- entry point – in an app, a function called only from the command line,
- family – a set of functions sharing common stem, e.g. family_subfunction
- library – a set of functions with zero or more families
- local function – a function used only within another function
- null operator – a shell command beginning with a
:
- semantic comment – a shell function which may not display its arguments, or display them on standard error
- shell script – a shell file, whose first line is a sh-bang:
#! env bash
- shdoc – a function family which implements Shell Documentation, see the Standard Function Library
- source – a builtin shell command to source or load a function library
- standard function – a function which meets the requirements of The Function Standard
- standard library – a library meeting the requirements of The Library Standard
- subfunction – a member of a function family
- tag – a colon-terminated first argument to the null command
1.5.3 Documents
These local papers are housed in the author's section of his commonplace book, which references The Function Standard
- Standards Documents
- Function Standard, The – Requirements for a standard bash shell function
- Library Standard, The – Requirements for a standard function library
- Application Requirements
- Shdoc Requirements – Requirments for the SHDOC application
- Application Requirements – Requirments for the APP builder application
- Practice Documents
- Standard Function Library – A Standard Function Library adhering to the Function and Library Standards, implementing functions which support the preferred behaviors
- Tag Manual – tag semantics, defining other tags, it's open-ended.
- Function Practice, A – Applying the lessons of the Standard Function Library
- External References
- Sh-bang – standard starting line of the script file