awk — Quoting
WRONG — shell expands $1 inside double quotes
cat <<'EOF' > /tmp/demo.txt
10.50.1.1 gateway UP
10.50.1.20 ise-01 UP
10.50.1.50 ad-dc DOWN
EOF
# WRONG — shell eats $1 before awk sees it
awk "{print $1}" /tmp/demo.txt
# CORRECT — single quotes protect awk variables
awk '{print $1}' /tmp/demo.txt
Pass shell variables safely with -v
pattern="DOWN"
awk -v pat="$pattern" '$0 ~ pat {print}' /tmp/demo.txt
WRONG — variable inside single quotes is literal
cat <<'EOF' > /tmp/demo.txt
2026-04-11 ERROR auth failure for user evan
2026-04-11 INFO session started
2026-04-11 ERROR certificate expired on ise-01
EOF
# WRONG — $var is literal text, not shell variable
var="ERROR"
awk '/$var/ {print}' /tmp/demo.txt
# CORRECT — use -v flag
awk -v pat="$var" '$0 ~ pat' /tmp/demo.txt
Regex in variable with slashes — use index() not ~
echo '/var/log/messages:auth failure' | \
awk -v pat="/var/log" 'index($0, pat)'
# index() returns position (truthy) for literal substring match
# ~ would choke on the unescaped slashes in the pattern
Pass multiple shell variables
cat <<'EOF' > /tmp/demo.txt
eth0 10.50.1.90 53 1200
eth0 10.50.1.20 443 85
eth1 10.50.1.1 80 3500
eth0 10.50.1.50 88 450
EOF
min=100
max=2000
awk -v lo="$min" -v hi="$max" '$4+0 >= lo && $4+0 <= hi' /tmp/demo.txt