
Simple example: use -v with a BEGIN block
This example sets a variable namedvar before the program executes and prints it from a BEGIN block. BEGIN runs before awk reads any input.
Notes on placement and behavior
- The
-vflag is an option to theawkcommand and is typically placed among other options. - For portability and clarity, place
-F(custom field separator) before-vwhen both are used. BEGINruns before any input is processed — use it for initialization, printing headers, or other tasks that don’t require input data.- If you omit
BEGINand 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)
Practical dataset: employees.txt
Use this sample data for the following examples. It’s pipe-delimited with fields:- ID
- First name
- Last name
- Department
- Job title
- 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):
-v prefix="..."defines a variable available inside the awk program.-F "|"splits records on the pipe character;$2is the first name.- The variable
prefixis 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:
Example — benchmark with two variables
Declare multiple variables by using multiple-v flags. This example finds employees earning <= 65000 or >= 90000:

Summary and best practices
| Topic | Recommendation | Example | |
|---|---|---|---|
| Variable declaration | Use -v name=value to set variables before awk runs | awk -v threshold=100 file | |
| Field separator | Place -F before -v for portability | `awk -F " | " -v n=1 ’…’` |
| Multiple variables | Use one -v per variable | -v a=1 -v b=2 | |
| Numeric comparisons | Quoted numeric strings work; awk converts strings to numbers when used numerically | -v n="90000" | |
| Initialization | Use BEGIN for startup tasks; omit BEGIN to process input records | BEGIN { print "Header" } |
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.-vseparates configuration from logic and improves clarity for one-liners and scripts.- Place
-Fbefore-vfor consistent behavior across awk implementations. - Use clear variable names and pass numeric thresholds via
-vto make scripts configurable. BEGINexecutes prior to record processing; normal action blocks execute per input record.
Links and references
- GNU Awk User’s Guide — comprehensive reference for awk options and behavior
- KornShell & awk examples — practical awk one-liners and tutorials
-v, how they interact with BEGIN blocks and regular action blocks, and practical examples using a sample employees file.