Variable Scope in AWK Functions (Global & Local)

In this tutorial, you’ll learn about variable scope in AWK functions.

We’ll explore how AWK handles variables in different contexts – whether they’re global, local, or parameters, and how this affects the values of variables.



Global Variables

Global variables are accessible from anywhere in your AWK script, including inside functions.

Here’s how you can access and modify a global variable inside a function:

#!/usr/bin/awk -f

# Define a global variable
totalCalls = 0

function addCalls(n) {
    totalCalls += n
    print "Total Calls: " + totalCalls


Total Calls: 5

In this script, totalCalls is a global variable. The function addCalls takes a number as an argument and adds it to totalCalls.

When addCalls(5) is called in the BEGIN block, it adds 5 to totalCalls.


Local Variables

When you define a function, you can declare local variables specific to that function.

These variables are only accessible within the function’s scope.

function myFunction() {
    local localVar = "I am local"
    print localVar


I am local

In this example, localVar is a local variable within myFunction. It holds the string “I am local” and is printed within the function.

Local variables in AWK exist only within the function where they are declared.

function anotherFunction() {
    local anotherVar = 5
    print anotherVar
print anotherVar


error: `anotherVar` is not defined

anotherVar is defined within anotherFunction and is accessible only there.

When you try to access it outside the function, you’ll get an error as it doesn’t exist outside the function’s scope.


Shadowing Global Variables with Local Variables

The local variable can have the same name as a global variable. This is known as shadowing.

The local variable “shadows” the global one within the function.

    globalVar = 10

function shadowFunction() {
    local globalVar = 20
    print "Inside function: ", globalVar

print "Outside function: ", globalVar


Inside function: 20
Outside function: 10

In this example, globalVar exists both globally and locally within shadowFunction.

Inside the function, the local globalVar (with value 20) takes precedence. Outside the function, the global globalVar (with value 10) is used.


Function Parameters and Arguments

In AWK, parameters of a function are treated as local variables within that function.

They are visible and accessible only within the function body.

function processRecord(column1, column2) {
    print "Processing:", column1, column2
    processRecord("Data1", "Data2")


Processing: Data1 Data2

Here, column1 and column2 are parameters of processRecord.

Inside the function, they are used to display the processed data.

Parameters in AWK are passed by value. This means changes to parameters inside the function do not affect the arguments passed in the calling scope.

function modifyValues(arg1, arg2) {
    arg1 = "Changed"
    print "Inside function:", arg1, arg2

    var1 = "Original"
    var2 = 10
    modifyValues(var1, var2)
    print "Outside function:", var1, var2


Inside function: Changed 10
Outside function: Original 10

In this example, although arg1 is modified within modifyValues, the original variable var1 outside the function remains unchanged.


Variable Scope and Recursion

Local variables within a recursive function are unique to each call of that function.

This is important for recursion to function correctly, as it ensures that each recursive call’s environment is distinct.

function complexRecursion(var1, depth) {
    local localVar = var1 * 2
    if (depth > 0) {
        print "Depth:", depth, "- localVar:", localVar
        complexRecursion(localVar, depth - 1)

    complexRecursion(1, 3)


Depth: 3 - localVar: 2
Depth: 2 - localVar: 4
Depth: 1 - localVar: 8

In this example, complexRecursion creates a new localVar each time it’s called.

The localVar value depends on the current var1 which changes with each recursive call.


Modify Built-in Variables in Functions

You can modify built-in variables in AWK such as FS (field separator), OFS (output field separator), NR (number of records processed), and others within functions just like any regular variable.

This change is reflected globally in the AWK script.

function modifyFieldSeparator() {
    FS = "|"
    print "Field Separator changed to |"

    print "Original Field Separator:", FS
    print "New Field Separator:", FS


Original Field Separator: 
Field Separator changed to |
New Field Separator: |


Variable Scope and Function Libraries

In a function library, shared variables are used to maintain state or pass data between functions.

These are global variables accessible by all functions within the library.

# Function Library: mylib.awk

# Global variable in the library
libraryVar = "shared data"

function setLibraryVar(newVal) {
    libraryVar = newVal

function getLibraryVar() {
    return libraryVar

Usage in an AWK script:

@include "mylib.awk"

    print getLibraryVar()
    setLibraryVar("new data")
    print getLibraryVar()


shared data
new data

In this example, libraryVar is a global variable used within the library mylib.awk.

It can be accessed and modified by the functions setLibraryVar and getLibraryVar.

Leave a Reply

Your email address will not be published. Required fields are marked *