Variable Manipulation and String Operations
Variable Manipulation and String Operations
The Set() application assigns and manipulates channel variables. Combined with Asterisk's string slicing syntax, it handles digit stripping, caller ID manipulation, and conditional routing.
Basic Variable Assignment
[examples]
exten => 6009,1,Set(COUNT=3)
same => n,SayNumber(${COUNT})
same => n,Set(COUNT=10)
same => n,SayNumber(${COUNT})
String Slicing: Strip Prefix Digits
[outbound]
; Strip the leading 9 from 9NXXXXXXX (access code dialing)
exten => _9NXXXXXXX,1,Set(number=${EXTEN:1})
same => n,Dial(PJSIP/trunk/${number})
; Strip a 3-digit prefix: 1015551234 -> 5551234
exten => _101XXXXXXX,1,Set(number=${EXTEN:3})
same => n,Dial(PJSIP/trunk/${number})
String Slicing: Extract Parts
[examples]
; Get the last 4 digits of a 10-digit number
exten => _NXXXXXXXXX,1,Set(last4=${EXTEN:-4})
; Get the area code (first 3 digits)
same => n,Set(areacode=${EXTEN:0:3})
; Get digits 4 through 6
same => n,Set(exchange=${EXTEN:3:3})
same => n,Verbose(1,Area: ${areacode} Exchange: ${exchange} Line: ${last4})
Variable Scope and Inheritance
[internal]
; Local variable -- only on this channel
exten => _1XXX,1,Set(LOCAL_VAR=hello)
; Single underscore -- inherits one level to the outbound channel
same => n,Set(_INHERIT_ONCE=world)
; Double underscore -- inherits to all child channels recursively
same => n,Set(__INHERIT_ALWAYS=foo)
same => n,Dial(PJSIP/${EXTEN})
How it works
${EXTEN:offset}: Slices the string starting atoffset.${EXTEN:1}removes the first character (digit 9 in9NXXXXXXX).${EXTEN:3}removes the first three characters.${EXTEN:offset:length}: Extractslengthcharacters starting atoffset.${EXTEN:0:3}gets the first 3 digits.${EXTEN:3:3}gets digits 4-6.${EXTEN:-N}: Negative offset counts from the end.${EXTEN:-4}returns the last 4 characters.- Variable inheritance: No prefix: variable is local to the current channel.
_prefix: inherits one level (to the channel created by Dial).__prefix: inherits to all child channels and their children. This is critical for features likeDYNAMIC_FEATURESandCALLERIDoverrides. - Pattern matching:
_9NXXXXXXXmatches a 9 followed by a number 2-9 then 7 digits.Xmatches 0-9,Zmatches 1-9,Nmatches 2-9,.matches one or more of anything,!matches one or more.
Tips
- Use
Verbose(1,Variable is: ${MYVAR})to debug variable values on the CLI. - Variables are case-sensitive:
${count}and${COUNT}are different. - For arithmetic:
Set(RESULT=$[${COUNT} + 1]): note the$[...]expression syntax. Set(ARRAY(VAR1,VAR2,VAR3)=a,b,c)sets multiple variables at once.- Channel variables disappear when the channel is hung up. For persistent storage, use
DB()(AstDB) orSet(CDR(userfield)=value). - Global variables (
[globals]section) are accessible to all channels but cannot be set per-channel.
User Notes
No notes yet. Be the first to contribute a tip or example.
Contribute a note
Share a tip, gotcha, or practical example. Keep it under 2000 characters. No questions (use the Asterisk community forums for support). Wrap code in backticks.