background preloader

Parametres

Facebook Twitter

Bash: Pass Shell Variables To awk. How do I pass shell variables to awk command or script under UNIX like operating systems?

Bash: Pass Shell Variables To awk

The -v option can be used to pass shell variables to awk command. Consider the following simple example, root="/webroot"echo | awk -v r=$root '{ print "shell root value - " r}' PASS parameter to AWK. ARGC and ARGV - The GNU Awk User's Guide. 7.5.3 Using ARGC and ARGV Auto-set, presented the following program describing the information contained in ARGC and ARGV: $ awk 'BEGIN { > for (i = 0; i < ARGC; i++) > print ARGV[i] > }' inventory-shipped mail-list -| awk -| inventory-shipped -| mail-list In this example, ARGV[0] contains ‘awk’, ARGV[1] contains ‘inventory-shipped’, and ARGV[2] contains ‘mail-list’.

ARGC and ARGV - The GNU Awk User's Guide

Notice that the awk program is not entered in ARGV. Passing parameters from shell to awk as an array.