awk

In multiline awk programs/scripts, actions preceded by BEGIN, a predicate (test-action), or END should insert a space before the opening curly brace ({). Single-line actions should insert a space after the opening curly and before the ending curly. Functions should insert a newline before/after the opening/closing curly brace. If the function can be written on a single-line in less than 80 characters, space can be used instead of newline.

function abc() { ... }
function def()
{
    ...
}
BEGIN { ... }
BEGIN {
    ...
}
{ ... }
{
    ...
}
test { ... }
test {
    ...
}
start, stop { ... }
start, stop {
    ...
}
END { ... }
END {
    ...
}

Localized function variables declared in the argument scope should be preceded by eight (8) spaces to separate them from the intended function arguments. If the arguments and locals together would exceed 79 characters, separate arguments and locals with a newline.

function abc(arg1, arg2,        local1, local2)
{
    ...
}
function def(arg1, arg2,
    local1, local2, local3, local4, local5, local6, local7, local8, local9,
    local10, local11)
{
    ...
}
function ghi(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10,
    arg11, arg12,
    local1, local2, local3)
{
    ...
}

Precede functions with intended syntax and description.

# add(arg1, arg2)
#
# Show addition. Returns sum of arg1 and arg2.
#
function add(arg1, arg2,        sum)
{
    sum = arg1 + arg2
    printf "%u + %u = %u\n", arg1, arg2, sum
    return int(sum)
}

Last updated