Using Variables in Asterisk Dialplans

Asterisk can make use of global, shared and channel-specific variables for arguments to commands. Variables are referenced in the dialplan (extensions.conf) using the syntax

${foo:offset:length}

where foo is the name of the variable, offset is an optional field indicating which characters should be excluded, and length is an optional field indicating the number of characters from the offset to be returned (see "Substrings" below for details and examples). A variable name may be any alphanumeric string beginning with a letter. User-defined variable names are not case sensitive — ${FOO} and ${Foo} refer to the same variable — but Asterisk-defined variables are case-sensitive — ${EXTEN} works, but ${exten} doesn't.

There are four types of variables: global variables, shared variables, channel variables, and environment variables.

Global variables can be set either in the [globals] category of extensions.conf or by using the SetGlobalVar command. Once defined, they can be referenced by any channel at any time.

Channel variables are set using the Set command (previously "setvar"). Each channel gets its own variable space, so there is no chance of collisions between different calls, and the variable is automatically trashed when the channel is hungup.

Environment variables provide a means to access unix environment variables from within Asterisk. There's a list further down this page.

If you define a channel variable with the same name as a global variable (and remember: user-defined variable names are not case sensitive), references to that variable name will return the value of the channel variable. For example, let us say that you define a context "FooTest" with a single extension, 100, with the following definition:(:smile:)

(Note the use of the NoOp command to assist in debugging.) If you dial extension 100 in context FooTest, and you have Asterisk running with a verbose console, you will see output similar to the following:

— Executing SetGlobalVar("Zap/1-1", "FOO=5") in new stack— Setting global variable 'FOO' to '5'— Executing NoOp("Zap/1-1", "5") in new stack— Executing NoOp("Zap/1-1", "5") in new stack— Executing Set("Zap/1-1", "foo=8") in new stack— Executing NoOp("Zap/1-1", "8") in new stack— Executing NoOp("Zap/1-1", "8") in new stack

We discover that after the call to SetGlobalVar, ${FOO} and ${foo} returned the value of the global variable, giving the value 5. After the call to Set, the global variable "foo" was obscured by the channel variable "foo"; ${FOO} and ${foo} both gave the value 8. The value of the global variable remains unchanged at 5, however, and any other channels that refer to the global variable ${foo} would still get the value 5.

Inheritance of Channel Variables

Prepending a single _ character to a variables name in Set will cause that variable to be inherited by channels created by the main channel. eg. when using Dial(Local/...); once inherited these variables will not be further inherited. Prepending two _ characters will cause them to be inherited indefinitely.

Note that for retrieval purposes these variable names do not include the underscores.

— Executing Set("SIP/oberon-365e", "FEE=fee") in new stack— Executing Set("SIP/oberon-365e", "_FIE=fie") in new stack— Executing Set("SIP/oberon-365e", "__FUM=fum") in new stack— Executing Dial("SIP/oberon-365e", "Local/105") in new stack— Called 105— Executing NoOp("Local/105@default-7263,2", "") in new stack— Executing NoOp("Local/105@default-7263,2", "fie") in new stack— Executing NoOp("Local/105@default-7263,2", "fum") in new stack— Executing Dial("Local/105@default-7263,2", "Local/106") in new stack— Called 106— Executing NoOp("Local/106@default-49be,2", "") in new stack— Executing NoOp("Local/106@default-49be,2", "") in new stack— Executing NoOp("Local/106@default-49be,2", "fum") in new stack

(DEPRECATED: As of release 1.2, with the addition of dialplan functions (which operate similarly to variables), the Set application has been renamed to Set. See http://www.voip-info.org/wiki/view/Asterisk+v1.2.0+upgrade

Using $

If you want to set a global variable containing another variable name in the [globals] category of extensions.conf you have to do something like this:

[globals]SS=$MY_VAR=${SS}{EPOCH}-${SS}{EXTEN}.gsm

This way the MY_VAR value is ${EPOCH}-${EXTEN}.gsm

Using it with the EVAL() function is very useful. I.e. if you want to record you can do this:

Predefined Channel Variables

There are some channel variables set by Asterisk that you can refer to in your dialplan definitions. Asterisk-defined variables, in contrast to user-defined variables, are case sensitive. Note: Several of these builtin variables have been converted to functions in 1.2, to allow setting their values.

${ACCOUNTCODE}: Account code, if specified - see Asterisk billing(DEPRECATED in 1.2.0 and removed in 1.4. Use ${CDR(accountcode)}

${ANSWEREDTIME}: This is the amount of time(in seconds) for actual call.

${BLINDTRANSFER}: The active SIP channel that dialed the number. This will return the SIP Channel that dialed the number when doing blind transfers - see BLINDTRANSFER

${CALLERID(all)}: The current Caller ID name and number - See Setting Callerid for usage in Asterisk 1.4

${CALLERID(name)}: The current Caller ID name - ${CALLERIDNAME} was used in versions of Asterisk prior to 1.2.0, it was DEPRECATED in 1.2.0 and removed in 1.4.

${CALLERID(num)}: The current Caller ID number - ${CALLERIDNUM} was used in versions of Asterisk prior to 1.2.0, it was DEPRECATED in 1.2.0 and removed in 1.4.

(Note: this is not necessarily numeric as the name would indicate and can legitimately contain the space character. Commands acting on this variable (such as 'GotoIf', for example) should be aware of this).

${DATETIME}: Current date time in the format: DDMMYYYY-HH:MM:SS This is deprecated in Asterisk 1.2, instead use :${STRFTIME(${EPOCH},,%d%m%Y-%H:%M:%S)})

${DIALEDPEERNAME}: Name of the called party. Broken for now, see DIALEDPEERNAME

${DIALEDPEERNUMBER}: Number of the called party. Broken for now, see DIALEDPEERNUMBER

${DIALEDTIME}: Time since the number was dialed (only works when dialed party answers the line?!)

${DIALSTATUS}: Status of the call. See DIALSTATUS(note: In the current SVN release, DIALSTATUS seems to have been removed. Now you should use the DEVSTATE function. Try in astersisk console "core show function DEVSTATE" for more informations)

${SIP_CODEC}: Set the SIP codec for the inbound (=first) call leg (see channelvariables.txt or README.variables in 1.2); Asterisk 1.6.2 also comes with SIP_CODEC_OUTBOUND for the remote (=second) call leg.

${SIPCALLID}: The SIP dialog Call-ID: header

${SIPUSERAGENT}: The SIP user agent header

${TIMESTAMP}: Current date time in the format: YYYYMMDD-HHMMSS This is deprecated as of Asterisk 1.4, instead use :${STRFTIME(${EPOCH},,%Y%m%d-%H%M%S)})

${TRANSFERCAPABILITY}: Type of Channel

${TXTCIDNAME}: Result of application TXTCIDName (see below)

${UNIQUEID}: Current call unique identifier

${TOUCH_MONITOR}: used for "one touch record" (see features.conf, and wW dial flags). If is set on either side of the call then that var contains the app_args for app_monitor otherwise the default of WAV||m is used

${TOUCH_MONITOR_PREFIX}: used for "one touch record" (see features.conf, and wW dial flags). This set Prefix to ${TOUCH_MONITOR} default: auto "New in 1.8"

Application-specific variables

Some applications take extra input or provide output using channel variables.

Variable math

exten => s,1,Set(SOMEVAR=$[${SOMEVAR} + 1]) ; incrementexten => s,2,Set(SOMEVAR=$[2 * ${SOMEVAR}]) ; multiplication etc...In times past, a single space was required between items in the $[...] expressions. This is no longer the case!

In late model Asterisks (1.2?), the MATH function is also available...

See also

Asterisk Expressions Asterisk has a powerful expression evaluator! It is called upon by wrapping an expression with $[ ... ] . Besides arithmetic expressions, there are some nice string manipulation capabilities.

Asterisk Functions Besides the ENV(), and LEN() functions, there are many more very useful functions. Some form the only method by which you can access or set certain facilities in Asterisk.