Skip to content

How Asterisk Works ​

This guide teaches you how to use expression evaluation in Asterisk, a key tool for evaluating arithmetic and logical expressions. It includes syntax, practical examples, important notes, and combined usage with variables.

Where to use these expressions

In uContact, $[...] expressions are used in the activities of telephony flows (IVRs, IVR functions and telephony flows), for example in Evaluate or Set variable. In Bots, conditions and values are written in JavaScript instead.

Basic Syntax ​

$[expr1 operator expr2]

Logical Operators ​

These operators allow you to combine boolean conditions:

  • expr1 | expr2 (Logical OR)

    • Returns expr1 if it evaluates as non-empty/non-zero ("true").
    • Otherwise, evaluates and returns expr2.
  • expr1 & expr2 (Logical AND)

    • Returns expr1 if both expressions evaluate as non-empty/non-zero ("true").
    • Otherwise, returns zero ("false").
  • !expr (Unary Logical NOT)

    • Note: There must be no space between ! and expr.

Using Logical Operators ​

You can combine conditions within GotoIf or similar commands:

$[ $[ "${FOO}" = "1" ] & $[ "${BAR}" = "2" ] ]

In this example, the expression evaluates whether both variables (FOO and BAR) meet their respective values. If they do, the full expression returns 1.

Comparison Operators ​

These allow value comparisons to control the call flow based on conditions:

  • expr1 = expr2
  • expr1 != expr2
  • expr1 < expr2
  • expr1 > expr2
  • expr1 <= expr2
  • expr1 >= expr2

Important Notes ​

  • If both arguments are integers, an integer comparison is performed.
  • Otherwise, a string comparison is performed using the system’s local sort order.
  • Returns 1 if true, 0 if false.

Arithmetic Operators ​

Asterisk supports basic arithmetic with integers and decimal numbers:

  • expr1 + expr2: Addition
  • expr1 - expr2: Subtraction
  • -expr: Negation
  • expr1 * expr2: Multiplication
  • expr1 / expr2: Division. The result can have decimals (for example, $[5 / 2] returns 2.5).
  • expr1 % expr2: Modulo (remainder)

To get an integer result, use the rounding functions described below (for example, $[TRUNC(5 / 2)] returns 2).

Other Operators ​

  • expr1 : expr2 (Regular expression match): compares expr1 with the regular expression expr2, anchored at the beginning. Returns the number of matched characters, or the text captured between \( and \) if the pattern has a group.
  • expr1 =~ expr2 (Regular expression match, not anchored): like :, but the match can be anywhere in expr1.
  • expr1 ? expr2 :: expr3 (Conditional): returns expr2 if expr1 is true (non-empty and non-zero), or expr3 otherwise.
    • Example: $[${COUNT} > 3 ? high :: low]

Asterisk Arithmetic Functions ​

Functions for rounding and truncation are available:

  • FLOOR(x): Rounds down to the nearest integer.
  • CEIL(x): Rounds up to the nearest integer.
  • ROUND(x): Rounds to the nearest integer. If exactly halfway, it rounds away from zero.
  • RINT(x): Rounds to the nearest integer, preferring the even number in case of a tie.
  • TRUNC(x): Removes the decimal part, rounding toward zero.

Clarifications ​

  • Dialplan functions can be called directly within $[...] without ${...}.

    • Example: $[3 + FLOOR(2.9)]
  • Variables, however, must use ${...}.

    • Example: $[${a} + 2]
  • Arguments in functions must be separated by commas , when using $[...].

Available Math Functions ​

Most functions from the C math library are available for more advanced calculations:

  • Trigonometric (input in radians):

    • COS(x)
    • SIN(x)
    • TAN(x)
  • Inverse Trigonometric (input between -1 and 1):

    • ACOS(x)
    • ASIN(x)
  • ATAN(x) (returns radians between -π/2 and π/2)

  • ATAN2(x, y) (returns between -π and π)

  • Exponents and Logarithms:

    • POW(x, y): x to the power of y (x^y)
    • EXP(x): e to the power of x
    • EXP2(x): 2 to the power of x
    • LOG(x): Natural logarithm (base e)
    • LOG2(x): Base 2 logarithm
    • LOG10(x): Base 10 logarithm
  • Others:

    • SQRT(x): Square root
    • REMAINDER(x, y): Returns the mathematical remainder (unlike %). Calculated as x - n*y where n = x/y rounded to the nearest even integer.