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.
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:
- Asterisk compares the dialed value with
_XXXX. Because2005contains four digits, the pattern matches. - Priority
1runsAnswer(), answering the call. - The next priority, represented by
n, evaluates${EXTEN}. It expands to2005. 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.
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:
- The caller dials
2005. - Asterisk evaluates the current context and searches for a matching extension.
- The pattern
_XXXXmatches the four digits. - Asterisk sets the current extension value represented by
EXTENto2005for this processing path. Answer()runs at priority 1.- At the next priority, variable expansion changes
${EXTEN}into2005. SayDigits()speaks the expanded digits.
Troubleshooting
The rule does not handle the number dialed
- Check whether the pattern matches the digit count.
_XXXXis 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
_XXXXmatches 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}is005.
The carrier receives the wrong digits
- Identify the exact access prefix callers enter.
- Validate the expanded
EXTENvalue 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.
CONTEXTidentifies the active dialplan context,EXTENidentifies the current extension, andPRIORITYidentifies 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
Xmatches one digit. Thus,_XXXXmatches 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.