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


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.

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

Related Posts

  • 54
    We often require to do string manipulation on a variable. For example, a variable named phonenumber which represents a number we'd like to call, and we want to strip off the first 5 digit before dialing the number. Asterisk provides a special syntax for doing just that, which looks like ${variable[:skip[:length]} The…
    Tags: asterisk, $, variable, variables
  • 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
    ChannelRedirect : This is used to redirect the given channel to target dialplan. For example if you want to redirect the channel SIP/1222-09992 to wrap context at 2 priority. You can use this function/command to do so. This has been added in Asterisk 1.4 version. For Asterisk 1.4 version ChannelRedirect(channel|[[context|]extension|]priority)…
    Tags: asterisk, channel, exten
  • 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
  • 36
    Dialplan Introduction:  
    Tags: 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: