Serious Shell Programming
  • Introduction
  • Acknowledgements
  • Basics
    • Strings
      • Single-Quotes
      • Double-Quotes
      • Unquoted Strings
      • Compound Strings
    • Here Documents
      • Here Doc
      • Indented Here Doc
      • Literal Here Doc
      • In-Memory Here Doc
    • Conditionals
      • Built-in test
      • Parameter Conditionals
      • Parameter test
    • Regex
      • grep
      • awk
      • pcre
    • Control Flow
      • Binary Operators
      • if-elif-else
      • case Statement
      • for Loop
      • while Loop
      • Functions
  • shellcheck
    • Introduction
    • Bad Advice
  • Style
    • awk
    • case
    • Redirection
    • Comments
    • trap
  • String Functions
    • substr
    • sprintf
    • replace
    • replaceall
    • replacestart
    • replaceend
    • fnmatch
  • awk
    • Pre-declaring Arrays
    • Sorting Arrays
  • Know Your limits
    • Arguments
    • Environment Variables
    • Solutions
Powered by GitBook
On this page

Was this helpful?

  1. Style

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)
}
PreviousBad AdviceNextcase

Last updated 5 years ago

Was this helpful?