With GNU awk
5.3.1 I want to assign a default value to a variable if it has not been given one on the command line. The value can be anything, including 0. I thought I could use SYMTAB
but as SYMTAB
is built during the parsing and before the execution it does not work:
$ awk -v v=1 'BEGIN { if(!("v" in SYMTAB)) v = 42; print "v = " v }' /dev/null
v = 1
$ awk 'BEGIN { if(!("v" in SYMTAB)) v = 42; print "v = " v }' /dev/null
v =
Is there another way?
The way to determine if a variable is set or not in any awk is by comparing it to both 0
and null
since the initial value for any scalar variable is 0-or-null
, there's no need for a gawk-specific SYMTAB[]
lookup (which you've already discovered you can't use for this as the variable v
will exist in SYMTAB[]
since it's mentioned in your code) or typeof()
call. For example:
$ awk -v v=1 'BEGIN { if((v=="") && (v==0)) v = 42; print "v = " v }'
v = 1
$ awk -v v=0 'BEGIN { if((v=="") && (v==0)) v = 42; print "v = " v }'
v = 0
$ awk -v v= 'BEGIN { if((v=="") && (v==0)) v = 42; print "v = " v }'
v =
$ awk 'BEGIN { if((v=="") && (v==0)) v = 42; print "v = " v }'
v = 42
FWIW that's also how you can tell if an optional parameter to a function was passed in or not.
By the way, in addition to portability, there's a functional difference between the above approach and using gawk for typeof(v) == "untyped"
as suggested elsewhere:
$ awk 'BEGIN { print "v = " v; if((v=="") && (v==0)) v = 42; print "v = " v }' /dev/null
v =
v = 42
$ awk 'BEGIN { print "v = " v; if(typeof(v) == "untyped") v = 42; print "v = " v }' /dev/null
v =
v =
If you're going to use typeof()
for this then you need to check for its output being either untyped
or unassigned
:
$ awk 'BEGIN { print "v = " v; if(typeof(v) ~ /^un(typed|assigned)$/) v = 42; print "v = " v }' /dev/null
v =
v = 42
See the gawk man page for more information on that :-).