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/

Related Posts

  • 44
    What is Asterisk? Asterisk is an open source framework for building communications applications. Asterisk turns an ordinary computer into a communications server. Asterisk powers IP PBX systems, VoIP gateways, conference servers and other custom solutions. It is used by small businesses, large businesses, call centers, carriers and government agencies, worldwide.…
    Tags: asterisk
  • 36
    Dialplan Introduction:  
    Tags: asterisk
  • 36
    JavaScript Variable Scope The scope of a variable is the region of your program in which it is defined. JavaScript variables have only two scopes. Global Variables− A global variable has global scope which means it can be defined anywhere in your JavaScript code. Local Variables− A local variable will…
    Tags: variable, global, variables
  • 35
    In Asterisk, a macro is a special kind of context which can be expanded within the definition of another context. Its name must begin with “macro-”. A macro is expanded by using the Macro command. All the lines of the macro definition should only use the s extension; the actual extension will come from the line in the calling context on…
    Tags: exten, asterisk

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

Your email address will not be published. Required fields are marked *

%d bloggers like this: