VMware ESXi and vSphere Cluster Management

Using Predefined Channel Variables in Asterisk Dialplans

Learn how Asterisk supplies CONTEXT, EXTEN, and PRIORITY, use ${EXTEN} in dialplans, match four-digit patterns, and strip prefixes with ${EXTEN:1}.

Asterisk dialplans use channel variables to carry values associated with a call channel. A predefined channel variable is supplied automatically by Asterisk while the call is being processed. These values describe the call's current routing state and location in dialplan execution.

This differs from a variable that you assign yourself with an application such as Set(). For example, a custom variable might store a customer number, while EXTEN is maintained by Asterisk to represent the extension currently being evaluated.

This lesson assumes familiarity with contexts, extensions, priorities, basic application syntax, and reloading dialplan configuration.

Core predefined channel variables

Variables are expanded with the syntax ${VARIABLE_NAME}. When Asterisk evaluates an application argument, it replaces that expression with the variable's current value.

Variable | Meaning | Typical dialplan useCONTEXT | The active dialplan context for the call. | Check or log which routing section is processing the call. EXTEN | The current extension or dialed extension being evaluated. | Reuse the dialed digits in applications and routing logic. PRIORITY | The current numbered or named execution position in the dialplan. | Identify the current step when debugging or controlling call flow.

CONTEXT, EXTEN, and PRIORITY describe where the call is and what Asterisk is currently doing. Their values can change as call processing moves through contexts, extensions, and priorities.

Using EXTEN in a dialplan

EXTEN lets one rule work with the value the caller dialed instead of hard-coding a particular number. If the caller enters 2005, an application argument containing ${EXTEN} expands to 2005.

One common use is passing the dialed digits to SayDigits(). This application speaks a sequence of digits, so it can confirm or announce the value currently being processed.

Matching a four-digit extension

An extension beginning with an underscore is an extension pattern, not a literal extension name. In a pattern, X matches one digit from 0 through 9. Therefore, _XXXX matches exactly four numeric digits.

exten => _XXXX,1,Answer()
same => n,SayDigits(${EXTEN})

For a caller who dials 2005, the execution order is:

  1. Asterisk compares the dialed value with _XXXX. Because 2005 contains four digits, the pattern matches.
  2. Priority 1 runs Answer(), answering the call.
  3. The next priority, represented by n, evaluates ${EXTEN}. It expands to 2005.
  4. SayDigits(2005) runs and speaks 2, 0, 0, 5.

The pattern handles many extensions with one rule. You do not need separate literal entries for 2000, 2001, 2002, and so on.

Understanding EXTEN substring syntax

A substring selects part of a variable's value. The syntax ${EXTEN:n} starts at the zero-based offset n from the left side of the value. The first character has offset 0, the second has offset 1, and so forth.

As a result, ${EXTEN:1} removes the first digit and returns every remaining digit. If EXTEN is 2005, the result is 005.

Dialed value | Expression | Expanded result | Explanation2005 | ${EXTEN} | 2005 | Returns the complete current extension. 2005 | ${EXTEN:1} | 005 | Starts at offset 1, so the first digit is omitted. 01234 | ${EXTEN:1} | 1234 | Removes the leading outbound access digit 0.

Speaking all but the first digit

The same pattern can demonstrate substring extraction:

exten => _XXXX,1,Answer()
same => n,SayDigits(${EXTEN:1})

With a matched value of 2005, the second application becomes SayDigits(005). The system speaks 0, 0, 5 because processing begins with the second character.

Prefix stripping for outbound routing

Prefix stripping means removing leading dialing digits before passing a number to another routing stage or an outbound carrier. A common plan requires users to dial 0 before an external destination.

For example, if a caller dials 01234, the leading 0 is an access prefix. The carrier should receive 1234, not 01234. Using ${EXTEN:1} produces the value beginning after that prefix.

; Conceptual outbound use
; EXTEN contains 01234
; Pass ${EXTEN:1} to the next outbound routing application

The exact outbound application depends on the rest of the dialplan, but the extraction is the same: ${EXTEN:1} changes the value from 01234 to 1234. If a plan uses two prefix digits, the offset must reflect that requirement.

Call-flow example

The following flow shows how a four-digit call can move through a pattern and use a predefined variable:

  1. The caller dials 2005.
  2. Asterisk evaluates the current context and searches for a matching extension.
  3. The pattern _XXXX matches the four digits.
  4. Asterisk sets the current extension value represented by EXTEN to 2005 for this processing path.
  5. Answer() runs at priority 1.
  6. At the next priority, variable expansion changes ${EXTEN} into 2005.
  7. SayDigits() speaks the expanded digits.

Troubleshooting

The rule does not handle the number dialed

  • Check whether the pattern matches the digit count. _XXXX is for exactly four numeric digits.
  • Verify the active CONTEXT. The call may have entered a different context than expected.
  • Confirm that a pattern is needed. A literal extension matches one specific value, while an expression such as _XXXX matches a range of values.

SayDigits speaks the full number

  • If the first digit should be omitted, use ${EXTEN:1} rather than ${EXTEN}.
  • Remember that the offset is zero-based: offset 0 is the first digit and offset 1 is the second.
  • Test with a known value such as 2005; the expected result for ${EXTEN:1} is 005.

The carrier receives the wrong digits

  • Identify the exact access prefix callers enter.
  • Validate the expanded EXTEN value before sending the call onward.
  • Ensure the substring offset removes exactly the intended number of leading digits. An offset of 1 removes one digit; it does not remove an arbitrary prefix.

Key points

  • Asterisk supplies predefined channel variables automatically during call processing.
  • CONTEXT identifies the active dialplan context, EXTEN identifies the current extension, and PRIORITY identifies the current execution step.
  • Use ${EXTEN} to reuse the caller's current extension without hard-coding it.
  • An underscore starts an extension pattern, and each X matches one digit. Thus, _XXXX matches four digits.
  • Use ${EXTEN:1} to remove the first digit, which is useful for outbound access-prefix stripping.

For related reference, see Predefined Channel Variables.