Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

Dialplan variables

Old News Asterisk The Open Source PBX
(main page)
Recommended Links Installation Reference Festival
Start and Stop Asterisk CLI Debugging Dialplan Basics Asterisk variables Users, Peers and Friends
Configuration Applications Examples of service Hardware requrements Humor Etc

Adapted from Asterisk variables - voip-info.org

Using Variables in Asterisk Dialplans

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 system variables are case-sensitive — ${EXTEN} works, but ${exten} doesn't.

There are four types of variables:

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:)

   [FooTest]
   exten => 100,1,SetGlobalVar(FOO=5)
   exten => 100,2,NoOp(${FOO})
   exten => 100,3,NoOp(${foo})
   exten => 100,4,SetVar(foo=8)
   exten => 100,5,NoOp(${FOO})
   exten => 100,6,NoOp(${foo})

(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 SetVar("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 SetVar, 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 SetVar 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:  for retrieval purposes these variable names do not include the underscores.

   [TestInherit]
   exten => 100,1,SetVar(__FOO=5)
   exten => 100,2,Dial(Local/test@TestInherit)
   exten => test,1,NoOp(${FOO})

will result in FOO being inherited. Without the underscores, the new local channel would start with a clean slate.
 

Example


   exten => 104,1,SetVar(FEE=fee)
   exten => 104,2,SetVar(_FIE=fie)
   exten => 104,3,SetVar(__FUM=fum)
   exten => 104,4,Dial(Local/105)

   exten => 105,1,NoOp(${FEE})
   exten => 105,2,NoOp(${FIE})
   exten => 105,3,NoOp(${FUM})
   exten => 105,4,Dial(Local/106)

   exten => 106,1,NoOp(${FEE})
   exten => 106,2,NoOp(${FIE})
   exten => 106,3,NoOp(${FUM})

results in

   — Executing SetVar("SIP/oberon-365e", "FEE=fee") in new stack
   — Executing SetVar("SIP/oberon-365e", "_FIE=fie") in new stack
   — Executing SetVar("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

(This did not work correctly prior to the 1.2 release.)
 

Using $

If you want to set a global variable containing the 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:

   exten => 104,1,SetVar(file=${EVAL(${MY_VAR})})
   exten => 104,2,MixMonitor($The attachment id given is not valid.)

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.
  (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).
 

Application-specific variables

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

Macro-specific variables

When in a macro context, extra channel variables are available.

Environment Variables

You may access unix environment variables using the syntax:

   ${ENV(foo)}

String Handling Functions

String Length


   ${LEN(foo)}

returns the length of the string foo. For example,

   exten => 100,1,SetVar(Fruit=pear)
   exten => 100,2,NoOp(${LEN(Fruit)})
   exten => 100,3,NoOp(${LEN(${Fruit})})

The first NoOp would show a value of 5 (the length of the string "fruit"). The second NoOp would show a value of 4 (the length of the string "pear").

This is an excellent way to check for a NULL or empty string.

Substrings

   ${foo:offset:length}

returns a substring of the string foo, beginning at offset offset and returning the next length characters. The first character is at offset 0.
 
Examples:

   ${123456789:1}        - returns the string 23456789
   ${123456789:-4}       - returns the string 6789
   ${123456789:0:3}      - returns the string 123
   ${123456789:2:3}      - returns the string 345
   ${123456789:-4:3}     - returns the string 678

Examples of use:

   exten => _NXX.,1,SetVar(areacode=${EXTEN:0:3})   - get the first 3 digits of ${EXTEN}
   exten => _516XXXXXXX,1,Dial(${EXTEN:3})          - get all but the first 3 digits of ${EXTEN}
   exten => 100,1,SetVar(whichVowel=4)

   exten => 100,2,SetVar(foo=AEIOU:${whichVowel}:1) - sets ${foo} to the single letter 'U'
 

String Concatenation

To concatenate two strings, simply write them together:
   ${foo}${bar}
   555${theNumber}
   ${longDistancePrefix}555${theNumber}

Variable math

To perform math on variables e.g. increment, multiplication, addition simply write:

exten => s,1,SetVar(SOMEVAR=$[${SOMEVAR} + 1]) ; increment
exten => s,2,SetVar(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...

exten => s,1,Set(SOMEVAR=${MATH(${SOMEVAR}+1)}) ; increment
exten => s,2,Set(SOMEVAR=${MATH(2*${SOMEVAR})}) ; multiplication etc...

Version notes

Recommended Links


 



Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to to buy a cup of coffee for authors of this site

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: March 12, 2019