Expressions apply operators to numeric and string operands, and return a result. They can be used in $[...] expression subs, the condition of /if and /while statements, and in /test.
The comparison operators return 0 for false, nonzero for true. The boolean operators stop evaluating as soon as the value of the expression is known ("short-circuit"). This does not affect the value of the expression, but is important when the second operand performs side effects.
Named variables may be accessed by simply using their name (with no leading '%'). This is called a variable reference.
Variable substitutions of the form "{var}" and "{var-default}" may be used to access any variable (named or positional). Note that there is no leading '%', and the '{' and '}' are required.
Variable substitutions beginning with '%' may also be used, but are not recommended, since the multiple '%'s required in nested macros can quickly get confusing. It always easier to use one of the above methods.
All operands will be automatically converted to the type expected by the
operator. String to integer conversion is done by interpreting leading
digits as an integer; e.g., "12ab" becomes 12, and "xyz" becomes 0.
Integer to string conversion is straightfoward. Enumerated
variables
(i.e., special variables that
are allowed to have only a limited set of values,
such as visual, which can only be "off"
or "on") are converted to strings
in a straightforward manner. Enumerated
variables are converted to
integers by having one integer stand for each of the allowed values. "Off"
is always 0, "on" is always "1", etc. This makes
"!visual
"
and "visual == 0
" the same
as "visual =~ 'off'
".
Other (non-enumerated) variable
references are treated as strings.
/set X=5 /set name=Hawkeye /set visual=1here are some expressions and their values:
Expression Value Comments ---- ----- -------- 3 + X * 2 13 3 + (5 * 2) = 13. "foo" =~ "bar" 0 "foo" is not identical to "bar". name =/ 'hawk*' 1 "Hawkeye" matches the glob "hawk*". X =~ "+5" 0 X is interpreted as string "5". X == "+5" 1 string "+5" is converted to integer 5. visual & (X > 0) 1 visual is nonzero, AND %X is positive.
See: functions, /test, evaluation, patterns