Asterisk can use global and channel variables in command arguments. To reference a variable in the dialplan (extensions.conf), use the following syntax:
${foo}
where foo is the variable name. A variable name can consist of any sequence of digits and letters, and must start with a letter. User-defined variable names are case-insensitive: ${FOO} and ${Foo} refer to the same variable; however, Asterisk variables are case-sensitive: ${EXTEN} works, but ${exten} does not.
There are three types of variables: global, channel, and environment variables.
Global variables are set either in the [global] section of the extensions.conf file or using the Set(GLOBAL(var)=value) command. A set variable is available to all channels at any time.
Channel variables are set using the Set command. Each channel has its own variable space, so there are no conflicts between channels, and the variable is automatically deleted when the channel ends.
Environment variables allow Asterisk to access UNIX environment variables. The list is below on this page.
If you define a channel variable with the same name as a global variable (remember: user-defined variable names are case-insensitive), references to this variable will return the channel variable's value. For example, you defined the context [FooTest] with a single extension 100:
[FooTest]
exten => 100,1,Set(GLOBAL(FOO)=5)
exten => 100,2,NoOp(${FOO})
exten => 100,3,NoOp(${foo})
exten => 100,4,Set(foo=8)
exten => 100,5,NoOp(${FOO})
exten => 100,6,NoOp(${foo})
When calling extension 100 in the [FooTest] context, the verbose console output will look something like this:
[2008-10-28 08:35:00] -- Executing [100@comlib-operator:1] Set("SIP/700-b6c28b40", "GLOBAL(FOO)=5") in new stack
[2008-10-28 08:35:00] == Setting global variable 'FOO' to '5'
[2008-10-28 08:35:00] -- Executing [100@comlib-operator:2] NoOp("SIP/700-b6c28b40", "5") in new stack
[2008-10-28 08:35:00] -- Executing [100@comlib-operator:3] NoOp("SIP/700-b6c28b40", "5") in new stack
[2008-10-28 08:35:00] -- Executing [100@comlib-operator:4] Set("SIP/700-b6c28b40", "foo=8") in new stack
[2008-10-28 08:35:00] -- Executing [100@comlib-operator:5] NoOp("SIP/700-b6c28b40", "8") in new stack
[2008-10-28 08:35:00] -- Executing [100@comlib-operator:6] NoOp("SIP/700-b6c28b40", "8") in new stack
We found that after calling Set(GLOBAL(FOO=5)), both ${FOO} and ${foo} return the value of the global variable, outputting 5. After calling Set(foo=5), the global variable FOO is hidden by the channel variable foo; both ${FOO} and ${foo} output the value 8. The value of the global variable remains unchanged at 5, and other channels referencing ${foo} will get the value 5.
Predefined channel variables
Asterisk defines several channel variables that can be referenced in the dialplan. Variable names defined by Asterisk are case-sensitive.
${CONTEXT}: Name of the current context
${DATETIME}: Current date and time in the format: !YYYY-MM-DD_HH:MM:SS
${TIMESTAMP}: Current date and time in the format: YYYYMMDD-HHMMSS
${EPOCH}: Current UNIX epoch time (number of seconds since 1 Jan 1970)
${EXTEN}: Current extension number
${INVALID_EXTEN}: Dialed extension number when redirected to the i (invalid) extension
${LANGUAGE}: Current language value. See Asterisk native language support
${PRIORITY}: Current priority
${CALLINGPRES}: PRI Call ID Presentation variable for incoming calls (See callingpres)
${CALLERID}: Calling identifier (name and number)
${CALLERIDNAME}: Calling identifier: name
${CALLERIDNUM}: Calling identifier: number
${CHANNEL}: Name of the current channel
${RDNIS}: The current redirecting DNIS, Caller ID that redirected the call. See RDNIS
${SIPDOMAIN}: SIP destination domain of an inbound call (if appropriate)
${SIP_CODEC}: Used to set the SIP codec for a call
${SIPCALLID}: The SIP dialog Call-ID: header
${SIPUSERAGENT}: The SIP user agent header
${UNIQUEID}: Current SIP call unique ID
${HANGUPCAUSE}: The last hangup return code on a Zap channel connected to a PRI interface
${DNID}: Dialed Number Identifier
${ACCOUNTCODE}: Account code, if specified - see Asterisk billing
