I've recently discovered that Awk's -v VAR=VAL syntax for initializing variables on the command line expands escape sequences in VAL. I previously thought that it was a good way to pass strings into Awk without needing to run an escaping function over them first.
For example, the following script:
awk -v VAR='x\tx' 'BEGIN{printf("%s\n", VAR);}'
I would expect to print
x\tx
but actually prints:
x x
An aside: environment variables to pass strings in unmodified instead, this question isn't asking how to get the behaviour I previously expected.
Here's what the man page has to say on the matter:
-v var=val, --assign var=val Assign the value val to the variable var, before execution of the program begins. Such variable values are available to the BEGIN block of an AWK program.
And further down:
String Constants String constants in AWK are sequences of characters enclosed between double quotes (like "value"). Within strings, certain escape sequences are recognized, as in C. These are:
... list of escape seqeuences ...
The escape sequences may also be used inside constant regular expressions (e.g., /[ \t\f\n\r\v]/ matches whitespace characters).
In compatibility mode, the characters represented by octal and hexadecimal escape sequences are treated literally when used in regular expression constants. Thus, /a\52b/ is equivalent to /a*b/.
The way I read this, val in -v var=val is not a string constant, and there is no text to indicate that the string constant escaping rules apply.
My questions:
- Is there a more authoritative source for the awk language than the man page, and if so what does it specify?
- What does POSIX have to say about this, if anything?
- Do all versions of Awk behave this way, i.e. can I rely on the expansion being done if I actually want it?
printf %sto avoid that. For more evidence try usinggsuborlength, the results indicate that the expansion is done before the awk script starts - je4d