Two strings can be concatenated using the @ operator, which creates
a new string simply gluing the two operands together:
When a string is used as an operand for an arithmetic operator, it is converted
to number. If the conversion fails (i.e. the string is not a number), the
value 0 is used - there is no error or warning emitted:
When the @ operator is used with a numeric operand, it is first converted
to string because the result of the @ operator is always a string:
NOTE: this section is interesting for programmers trying to understand
libfawk implementation internals. Reference counting does not have any
visible effect for scripting.
The only object in
However, it makes strings immutable (read-only): instead of making a
modification to any existing string, any modification needs to be done on
a newly allocated string - sort of a Copy-On-Write.
fawk_print_cell() prints the reference counter.
Substrings can be generated using the AWK standard
substr() builtin:
In many cases the script will need to print or build strings, from
static string literals and dynamic calculated data. Most often the ratio
of string literals and calculated data allows a script written with the
traditional approach look good. However, in some rare cases there are huge
blocks of verbatim string data, often with newlines and indentation, that
requires only a few fields to be filled in.
A typical example is generating a Makefile like this, with an option
to replace -O3 with -g:
With the usual string tools, storing the Makefile template in the script
would make it unreadable because of the \n newlines and the tab indentation
wouldn't be obvious either.
The first character after the [[ is the escape character, then the string
starts. A string lasts until the escape character. If the escape character
is followed by a ]], the text block is closed, else the parser switches mode
and parses an expression until the next escape character, where it switches
back to string mode. Any number of strings and expressions may be put in
a text block, in arbitrary order.
At the end of the day, the string parts are converted to string literals
and the whole text block is converted into an expression that concatenates
its parts with the string concatenation operator (@). In the second syntax
example above, this means: "string1" @ expr @ "string2".
Note: since every character of the string part is copied verbatim, except
for the escape character, the text block may contain spaces, tabs, newlines,
non-printable characters (other than \0).
Note: empty expr is not accepted (because it would lead to "str" @ @ "str",
which is invalid).
The programmer is free to choose the escape character: it can be any
ASCII character. The only restriction is that once an escape character is
chosen for a text block, that character can not be used in the string or
expression part, because it is always interpreted as an escape character -
there's no escaping of the escape character.
Typical choices for the escape character:
The above Makefile example using a text block:
In this example ^ was chosen for escape because it normally does not
appear in make(1) syntax. The expression,
7.2. String conversions
7.3. Reference counting
7.4. Substrings
7.5. Text blocks
LDFLAGS =
LDLIBS = -lm
CFLAGS = -I.. -Wall -O3
all: main
main: main.o
$(CC) $(LDFLAGS) -o main main.o $(LDLIBS)
main.o: main.c
$(CC) -c $(CFLAGS) -o main.o main.c
[[~string~]]
or
[[~string1~expr~string2~]]
or in general:
[[~ string or ~expr~ ... ~]]