Skip to main content
The -v option in awk sets variables before the awk program or action block runs. Declaring variables this way keeps configuration separate from program logic, makes one-liners easier to read, and improves reusability for scripts and pipelines.
A presentation slide titled "awk -v Variables" with a large "$ awk" logo in the center. Below it is the subtitle "Domain-Specific Language" inside a dotted rounded rectangle.

Simple example: use -v with a BEGIN block

This example sets a variable named var before the program executes and prints it from a BEGIN block. BEGIN runs before awk reads any input.

Notes on placement and behavior

  • The -v flag is an option to the awk command and is typically placed among other options.
  • For portability and clarity, place -F (custom field separator) before -v when both are used.
  • BEGIN runs before any input is processed — use it for initialization, printing headers, or other tasks that don’t require input data.
  • If you omit BEGIN and use an action like { print var }, awk reads input (files or stdin) and executes the action for every input record. End interactive stdin with Ctrl+D on Unix-like systems.
When you need configuration values that may change (thresholds, prefixes, file names), pass them with -v instead of hard-coding them in the awk program — it improves readability and allows reusing the same script with different settings.

Example — printing a variable for each input line (stdin)

(Press Ctrl+D to finish interactive stdin input.)

Practical dataset: employees.txt

Use this sample data for the following examples. It’s pipe-delimited with fields:
  1. ID
  2. First name
  3. Last name
  4. Department
  5. Job title
  6. Email
  7. Salary

Example — prefix first names with a descriptive variable

Use -F "|" -v prefix="..." to set the field separator and a descriptive prefix that will print before the first name field ($2):
Observations:
  • -v prefix="..." defines a variable available inside the awk program.
  • -F "|" splits records on the pipe character; $2 is the first name.
  • The variable prefix is expanded inside the action block for each input record.

Filtering by numeric field (salary)

Print employees earning 90,000 or more (salary is field $7):

Parameterize the threshold with -v

Use -v to pass the salary threshold, making the command reusable without editing the awk program:
Print only the first names of high earners:

Example — benchmark with two variables

Declare multiple variables by using multiple -v flags. This example finds employees earning <= 65000 or >= 90000:
A dark-themed slide titled "awk -v Variables" showing a central "-v" icon and text advising to create meaningful variable names because they help quickly identify the program's purpose.

Summary and best practices

Each -v sets a variable before awk begins. If you pass a variable name that matches an awk built-in or field name, you can shadow it — choose meaningful names (e.g., high_salary, prefix) to avoid collisions and improve maintainability.
Key takeaways:
  • -v separates configuration from logic and improves clarity for one-liners and scripts.
  • Place -F before -v for consistent behavior across awk implementations.
  • Use clear variable names and pass numeric thresholds via -v to make scripts configurable.
  • BEGIN executes prior to record processing; normal action blocks execute per input record.
This lesson covered declaring and using variables in awk with -v, how they interact with BEGIN blocks and regular action blocks, and practical examples using a sample employees file.

Watch Video