Asterisk – Understanding Asterisk Variable

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.

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 SetGlobalVarcommand. Once defined, they can be referenced by any channel at any time.
  • Shared variables are new in Asterisk 1.6: Two (or more?) channels can gain full access with the help of Asterisk func SHARED to what otherwise would be a channel variable.
  • Channel variables are set using the Set command (previously “setvar”). Eachchannel 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. Let’s have an example:

Here we are defining a context test with a fixed single extension 100.

[test]
exten => 100,1,SetGlobalVar(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})

Now if we dial extension 100, will see the output like :

— Executing SetGlobalVar(“SIP/1-1”, “FOO=5”) in new stack
— Setting global variable ‘FOO’ to ‘5’
— Executing NoOp(“SIP/1-1”, “5”) in new stack
— Executing NoOp(“SIP/1-1”, “5”) in new stack
— Executing Set(“SIP/1-1”, “foo=8”) in new stack
— Executing NoOp(“SIP/1-1”, “8”) in new stack
— Executing NoOp(“SIP/1-1”, “8”) in new stack

To Details about Asterisk variable Click Here

http://blog.eduguru.in/asterisk-dialplan-varriable/

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply