Environment variables in AWK
Like other programming environments, AWK also has environment variables. In this section, we will discuss the different environment variables available to users in the AWK programming language.
ARGC and ARGV
The ARGC
and ARGV
variables are used to pass arguments to the AWK script from the command line.
ARGC
specifies the total number of arguments passed to the AWK script on the command line. It always hasa value of 1 or more, as it counts the program name as the first argument.
The AWK script filename specified using the -f
option is not counted as an argument. If we declare any variable on the command line, it is counted as an argument in GAWK:
$ awk 'BEGIN { print "No of arguments =", ARGC }' one two three four
The output of the execution of the preceding command is as follows:
No of arguments = 5
ARGV
is an array that storesall the arguments passed to the AWK command, starting from index 0 through to ARGC
. ARGV[0]
always contains AWK.
In the following example, we show...